Enum ip_network::IpNetwork

source ·
pub enum IpNetwork {
    V4(Ipv4Network),
    V6(Ipv6Network),
}
Expand description

Holds IPv4 or IPv6 network.

Variants§

Implementations§

source§

impl IpNetwork

source

pub fn new<I: Into<IpAddr>>( network_address: I, netmask: u8, ) -> Result<Self, IpNetworkError>

Constructs new IpNetwork based on IpAddr and netmask.

§Examples
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
use ip_network::{IpNetwork, Ipv4Network};

let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0));
let ip_network = IpNetwork::new(network_address, 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
assert_eq!(ip_network.netmask(), 24);
source

pub fn new_truncate<I: Into<IpAddr>>( network_address: I, netmask: u8, ) -> Result<Self, IpNetworkError>

Constructs new IpNetwork based on IpAddr and netmask with truncating host bits from given network_address.

Returns error if netmask is bigger than 32 for IPv4 and 128 for IPv6.

§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;

let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 128));
let ip_network = IpNetwork::new_truncate(network_address, 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
assert_eq!(ip_network.netmask(), 24);
source

pub fn network_address(&self) -> IpAddr

Returns network IP address.

§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;

let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
source

pub fn netmask(&self) -> u8

Returns network mask as integer.

§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;

let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.netmask(), 24);
source

pub fn is_ipv4(&self) -> bool

Returns true if IpNetwork contains Ipv4Network struct.

source

pub fn is_ipv6(&self) -> bool

Returns true if IpNetwork contains Ipv6Network struct.

source

pub fn contains<I: Into<IpAddr>>(&self, ip: I) -> bool

Returns true if IpNetwork contains IpAddr. For different network type (for example IpNetwork is IPv6 and IpAddr is IPv4) always returns false.

§Examples
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use ip_network::IpNetwork;

let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert!(ip_network.contains(Ipv4Addr::new(192, 168, 1, 25)));
assert!(!ip_network.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 1, 0, 0)));
source

pub fn is_default_route(&self) -> bool

Returns true if the network is default route, that contains all IP addresses.

source

pub fn is_multicast(&self) -> bool

Returns true if the network is part of multicast network range.

source

pub fn is_documentation(&self) -> bool

Returns true if this is a part of network reserved for documentation.

source

pub fn is_loopback(&self) -> bool

Returns true if this network is inside loopback address range.

source

pub fn is_global(&self) -> bool

Returns true if the network appears to be globally routable.

source

pub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>

Converts string in format IPv4 (X.X.X.X/Y) or IPv6 (X:X::X/Y) CIDR notation to IpNetwork.

§Examples
use std::net::Ipv4Addr;
use ip_network::{IpNetwork, Ipv4Network};

let ip_network = IpNetwork::from_str_truncate("192.168.1.1/24").unwrap();
assert_eq!(ip_network, IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap()));
source

pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>

Return an iterator of the collapsed IpNetworks.

Trait Implementations§

source§

impl Clone for IpNetwork

source§

fn clone(&self) -> IpNetwork

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for IpNetwork

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for IpNetwork

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Converts IpNetwork to string in format X.X.X.X/Y for IPv4 and X:X::X/Y for IPv6 (CIDR notation).

§Examples
use std::net::Ipv4Addr;
use ip_network::{IpNetwork, Ipv4Network};

let ip_network = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?);
assert_eq!(ip_network.to_string(), "192.168.1.0/24");
source§

impl From<IpAddr> for IpNetwork

source§

fn from(ip: IpAddr) -> Self

Converts IpAddr to IpNetwork with netmask 32 for IPv4 address and 128 for IPv6 address.

source§

impl From<Ipv4Addr> for IpNetwork

source§

fn from(ip: Ipv4Addr) -> Self

Converts Ipv4Addr to IpNetwork with netmask 32.

source§

impl From<Ipv4Network> for IpNetwork

source§

fn from(network: Ipv4Network) -> Self

Converts to this type from the input type.
source§

impl From<Ipv6Addr> for IpNetwork

source§

fn from(ip: Ipv6Addr) -> Self

Converts Ipv6Addr to IpNetwork with netmask 128.

source§

impl From<Ipv6Network> for IpNetwork

source§

fn from(network: Ipv6Network) -> Self

Converts to this type from the input type.
source§

impl FromStr for IpNetwork

source§

fn from_str(s: &str) -> Result<IpNetwork, IpNetworkParseError>

Converts string in format IPv4 (X.X.X.X/Y) or IPv6 (X:X::X/Y) CIDR notation to IpNetwork.

§Examples
use std::net::Ipv4Addr;
use std::str::FromStr;
use ip_network::{IpNetwork, Ipv4Network};

let ip_network = IpNetwork::from_str("192.168.1.0/24").unwrap();
assert_eq!(ip_network, IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap()));
§

type Err = IpNetworkParseError

The associated error which can be returned from parsing.
source§

impl Hash for IpNetwork

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Ord for IpNetwork

source§

fn cmp(&self, other: &IpNetwork) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<IpNetwork> for Ipv4Network

source§

fn eq(&self, other: &IpNetwork) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<IpNetwork> for Ipv6Network

source§

fn eq(&self, other: &IpNetwork) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Ipv4Network> for IpNetwork

source§

fn eq(&self, other: &Ipv4Network) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Ipv6Network> for IpNetwork

source§

fn eq(&self, other: &Ipv6Network) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for IpNetwork

source§

fn eq(&self, other: &IpNetwork) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<IpNetwork> for Ipv4Network

source§

fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd<IpNetwork> for Ipv6Network

source§

fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd<Ipv4Network> for IpNetwork

source§

fn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd<Ipv6Network> for IpNetwork

source§

fn partial_cmp(&self, other: &Ipv6Network) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd for IpNetwork

source§

fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

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

impl Copy for IpNetwork

source§

impl Eq for IpNetwork

source§

impl StructuralPartialEq for IpNetwork

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.