pub enum IpNetwork {
V4(Ipv4Network),
V6(Ipv6Network),
}
Expand description
Represents a generic network range. This type can have two variants: the v4 and the v6 case.
Variants
V4(Ipv4Network)
V6(Ipv6Network)
Implementations
sourceimpl IpNetwork
impl IpNetwork
sourcepub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>
pub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>
Constructs a new IpNetwork
from a given IpAddr
and a prefix denoting the
network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this
will raise an IpNetworkError::InvalidPrefix
error. Support for IPv6 is not
complete yet.
sourcepub fn prefix(&self) -> u8
pub fn prefix(&self) -> u8
Returns the prefix of the given IpNetwork
Example
use ipnetwork::IpNetwork;
assert_eq!(IpNetwork::V4("10.9.0.32/16".parse().unwrap()).prefix(), 16u8);
assert_eq!(IpNetwork::V6("ff01::0/32".parse().unwrap()).prefix(), 32u8);
sourcepub fn mask(&self) -> IpAddr
pub fn mask(&self) -> IpAddr
Returns the mask for this IpNetwork
.
That means the prefix
most significant bits will be 1 and the rest 0
Example
use ipnetwork::IpNetwork;
use std::net::{Ipv4Addr, Ipv6Addr};
let v4_net: IpNetwork = "10.9.0.32/16".parse().unwrap();
assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 0, 0));
let v6_net: IpNetwork = "ff01::0/32".parse().unwrap();
assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));
sourcepub fn is_ipv4(&self) -> bool
pub fn is_ipv4(&self) -> bool
Returns true if the IP in this IpNetwork
is a valid IPv4 address,
false if it’s a valid IPv6 address.
Example
use ipnetwork::IpNetwork;
let v4: IpNetwork = IpNetwork::V4("10.9.0.32/16".parse().unwrap());
assert_eq!(v4.is_ipv4(), true);
assert_eq!(v4.is_ipv6(), false);
sourcepub fn is_ipv6(&self) -> bool
pub fn is_ipv6(&self) -> bool
Returns true if the IP in this IpNetwork
is a valid IPv6 address,
false if it’s a valid IPv4 address.
Example
use ipnetwork::IpNetwork;
let v6: IpNetwork = IpNetwork::V6("ff01::0/32".parse().unwrap());
assert_eq!(v6.is_ipv6(), true);
assert_eq!(v6.is_ipv4(), false);
sourcepub fn contains(&self, ip: IpAddr) -> bool
pub fn contains(&self, ip: IpAddr) -> bool
Checks if a given IpAddr
is in this IpNetwork
Examples
use std::net::IpAddr;
use ipnetwork::IpNetwork;
let net: IpNetwork = "127.0.0.0/24".parse().unwrap();
let ip1: IpAddr = "127.0.0.1".parse().unwrap();
let ip2: IpAddr = "172.0.0.1".parse().unwrap();
let ip4: IpAddr = "::1".parse().unwrap();
assert!(net.contains(ip1));
assert!(!net.contains(ip2));
assert!(!net.contains(ip4));
Trait Implementations
sourceimpl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
sourcefn from(v4: Ipv4Network) -> IpNetwork
fn from(v4: Ipv4Network) -> IpNetwork
Performs the conversion.
sourceimpl From<Ipv6Network> for IpNetwork
impl From<Ipv6Network> for IpNetwork
sourcefn from(v6: Ipv6Network) -> IpNetwork
fn from(v6: Ipv6Network) -> IpNetwork
Performs the conversion.
sourceimpl FromStr for IpNetwork
impl FromStr for IpNetwork
Tries to parse the given string into a IpNetwork
. Will first try to parse
it as an Ipv4Network
and if that fails as an Ipv6Network
. If both
fails it will return an InvalidAddr
error.
Examples
use std::net::Ipv4Addr;
use ipnetwork::{IpNetwork, Ipv4Network};
let expected = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(10, 1, 9, 32), 16).unwrap());
let from_cidr: IpNetwork = "10.1.9.32/16".parse().unwrap();
assert_eq!(expected, from_cidr);
type Err = IpNetworkError
type Err = IpNetworkError
The associated error which can be returned from parsing.
sourceimpl Ord for IpNetwork
impl Ord for IpNetwork
sourceimpl PartialOrd<IpNetwork> for IpNetwork
impl PartialOrd<IpNetwork> for IpNetwork
sourcefn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
impl Copy for IpNetwork
impl Eq for IpNetwork
impl StructuralEq for IpNetwork
impl StructuralPartialEq for IpNetwork
Auto Trait Implementations
impl RefUnwindSafe for IpNetwork
impl Send for IpNetwork
impl Sync for IpNetwork
impl Unpin for IpNetwork
impl UnwindSafe for IpNetwork
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more