Enum ipnetwork::IpNetwork [−][src]
Represents a generic network range. This type can have two variants: the v4 and the v6 case.
Variants
V4(Ipv4Network)
V6(Ipv6Network)
Implementations
impl IpNetwork
[src]
pub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>
[src]
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.
pub fn ip(&self) -> IpAddr
[src]
Returns the IP part of a given IpNetwork
pub fn prefix(&self) -> u8
[src]
Returns the prefix of the given IpNetwork
Example
use ipnetwork::IpNetwork; assert_eq!(IpNetwork::V4("10.9.0.1".parse().unwrap()).prefix(), 32u8); assert_eq!(IpNetwork::V4("10.9.0.32/16".parse().unwrap()).prefix(), 16u8); assert_eq!(IpNetwork::V6("ff01::0".parse().unwrap()).prefix(), 128u8); assert_eq!(IpNetwork::V6("ff01::0/32".parse().unwrap()).prefix(), 32u8);
pub fn network(&self) -> IpAddr
[src]
Returns the address of the network denoted by this IpNetwork
.
This means the lowest possible IP address inside of the network.
Examples
use std::net::{Ipv4Addr, Ipv6Addr}; use ipnetwork::IpNetwork; let net: IpNetwork = "10.1.9.32/16".parse().unwrap(); assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0)); let net: IpNetwork = "2001:db8::/96".parse().unwrap(); assert_eq!(net.network(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
pub fn broadcast(&self) -> IpAddr
[src]
Returns the broadcasting address of this IpNetwork
.
This means the highest possible IP address inside of the network.
Examples
use std::net::Ipv4Addr; use ipnetwork::{IpNetwork, Ipv4Network}; let net: Ipv4Network = "10.9.0.32/16".parse().unwrap(); assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));
pub fn mask(&self) -> IpAddr
[src]
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.1".parse().unwrap(); assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 255, 255)); 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".parse().unwrap(); assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)); let v6_net: IpNetwork = "ff01::0/32".parse().unwrap(); assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));
pub fn is_ipv4(&self) -> bool
[src]
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);
pub fn is_ipv6(&self) -> bool
[src]
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);
pub fn contains(&self, ip: IpAddr) -> bool
[src]
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));
pub fn size(&self) -> NetworkSize
[src]
Returns the number of possible host addresses in this IpAddr
Examples
use ipnetwork::{IpNetwork, NetworkSize}; let net: IpNetwork = "127.0.0.0/24".parse().unwrap(); assert_eq!(net.size(), NetworkSize::V4(256))
Trait Implementations
impl Clone for IpNetwork
[src]
impl Copy for IpNetwork
[src]
impl Debug for IpNetwork
[src]
impl<'de> Deserialize<'de> for IpNetwork
[src]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
impl Display for IpNetwork
[src]
impl Eq for IpNetwork
[src]
impl From<IpAddr> for IpNetwork
[src]
impl From<Ipv4Network> for IpNetwork
[src]
fn from(v4: Ipv4Network) -> IpNetwork
[src]
impl From<Ipv6Network> for IpNetwork
[src]
fn from(v6: Ipv6Network) -> IpNetwork
[src]
impl FromStr for IpNetwork
[src]
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
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<IpNetwork, IpNetworkError>
[src]
impl Hash for IpNetwork
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for IpNetwork
[src]
fn cmp(&self, other: &IpNetwork) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<IpNetwork> for IpNetwork
[src]
impl PartialOrd<IpNetwork> for IpNetwork
[src]
fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl Serialize for IpNetwork
[src]
impl StructuralEq for IpNetwork
[src]
impl StructuralPartialEq for IpNetwork
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,