Enum net_adds::IpAddrNetwork [−][src]
pub enum IpAddrNetwork { V4(Ipv4AddrNetwork), V6(Ipv6AddrNetwork), }
Expand description
An IP address network, either IPv4 or IPv6.
This enum can contain either an Ipv4AddrNetwork
or an Ipv6AddrNetwork
, see their
respective documentation for more details.
The size of an IpAddrNetwork
struct may vary depending on the target operating system.
Textual representation
IpAddrNetwork
provides a FromStr
implementation. The two parts are divided by /
.
For IPv4, the first part must contain an IPv4. The second part can either contain an IPv4 or an u8 between 0 and 32 which is valid as a netmask prefix.
For IPv6, the first part must contain an IPv6. The second part can either contain an IPv6 or an u8 between 0 and 128 which is valid as a netmask prefix.
Examples
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); assert!(network.is_ipv4()); assert_eq!(Ok(network), "192.168.0.10/24".parse()); assert_eq!(Ok(network), "192.168.0.10/255.255.255.0".parse()); let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new_with_addr(Ipv6Addr::from(0x1), netmask).unwrap()); assert!(network.is_ipv6()); assert_eq!(Ok(network), "::1/120".parse()); assert_eq!(Ok(network), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse());
Variants
V4(Ipv4AddrNetwork)
V6(Ipv6AddrNetwork)
Implementations
Returns all ip of the network including the network and the broadcast addr.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); assert_eq!(network.all(), vec![ Ipv4Addr::new(192, 162, 0, 8), Ipv4Addr::new(192, 162, 0, 9), Ipv4Addr::new(192, 162, 0, 10), Ipv4Addr::new(192, 162, 0, 11) ]); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); assert_eq!(network.all(), vec![ Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3) ]);
Returns all hosts (exclude network & broadcast addr).
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); assert_eq!(network.hosts(), vec![ Ipv4Addr::new(192, 162, 0, 9), Ipv4Addr::new(192, 162, 0, 10) ]); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); assert_eq!(network.hosts(), vec![ Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2) ]);
Returns the number of ip’s included in the network including the network and the broadcast addr.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); assert_eq!(network.size(), 256); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap()); assert_eq!(network.size(), 256);
Returns true if the ip argument is included in the network, else returns false.
Panic if IPv4 and IPv6 are mixed.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap()); assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 0)))); assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 142)))); assert!(network.has(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 255)))); assert!(!network.has(IpAddr::V4(Ipv4Addr::new(192, 169, 0, 0)))); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 64).unwrap()); assert!(network.has(IpAddr::V6(Ipv6Addr::from(0x1)))); assert!(network.has(IpAddr::V6(Ipv6Addr::from(0xA)))); assert!(network.has(IpAddr::V6(Ipv6Addr::from(0x00FF)))); assert!(!network.has(IpAddr::V6(Ipv6Addr::from(0xFFFFFFFFFFFFFFFFFF00000000000000))));
Returns true if the network contains IPv4, else return false.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); assert!(network.is_ipv4()); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); assert!(!network.is_ipv4());
Returns true if the network contains IPv6, else return false.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap()); assert!(network.is_ipv6()); let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 162, 0, 10), 30).unwrap()); assert!(!network.is_ipv6());
Trait Implementations
Create an IpAddrNetwork::V4
from an Ipv4AddrNetwork
.
Examples:
use std::net::Ipv4Addr; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork}; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap(); assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V4(network));
Create an IpAddrNetwork::V6
from an Ipv6AddrNetwork
.
Examples:
use std::net::Ipv6Addr; use net_adds::{IpAddrNetwork, Ipv6AddrNetwork}; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap(); assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V6(network));
Parse a string as IpAddrNetwork
.
If the string representation is not valid return an NetAddsErrorAddrParse(NetworkAddrParseError)
.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let ip = Ipv4Addr::new(192, 168, 0, 0); let network = IpAddrNetwork::V4(Ipv4AddrNetwork::try_new(ip, 24).unwrap()); assert_eq!(Ok(network), "192.168.0.0/24".parse()); assert_eq!(Ok(network), "192.168.0.0/255.255.255.0".parse()); let ip = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFF); let network = IpAddrNetwork::V6(Ipv6AddrNetwork::try_new(ip, 120).unwrap()); assert_eq!(Ok(network), "ffff::ff/120".parse()); assert_eq!(Ok(network), "ffff::ff/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse());
type Err = NetAddsError
type Err = NetAddsError
The associated error which can be returned from parsing.
Create a IpAddr
iterator.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); let mut iter = IpAddrNetwork::V4(network).into_iter(); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 8)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 9)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 11)))); assert_eq!(iter.next(), None); let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); let mut iter = IpAddrNetwork::V6(network).into_iter(); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)))); assert_eq!(iter.next(), None);
Create a smart IpAddr
iterator.
Examples:
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork, Ipv6AddrNetwork}; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); let mut iter = IpAddrNetwork::V4(network).into_iter(); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 8)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 9)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)))); assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 11)))); assert_eq!(iter.next(), None); let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); let mut iter = IpAddrNetwork::V6(network).into_iter(); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)))); assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)))); assert_eq!(iter.next(), None);
type IntoSmartIter = IpAddrSmartIterator
type IntoSmartIter = IpAddrSmartIterator
Which kind of iterator are we turning this into?
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for IpAddrNetwork
impl Send for IpAddrNetwork
impl Sync for IpAddrNetwork
impl Unpin for IpAddrNetwork
impl UnwindSafe for IpAddrNetwork
Blanket Implementations
Mutably borrows from an owned value. Read more