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 the ip addr.

Returns the netmask prefix.

Returns the netmask addr.

Returns the network addr.

Returns the broadcast addr.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

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());

The associated error which can be returned from parsing.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

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);

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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);

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.