pub struct Ipv4Network { /* private fields */ }ipnetwork only.Expand description
Represents a network range where the IP addresses are of v4
Implementationsยง
Sourceยงimpl Ipv4Network
impl Ipv4Network
Sourcepub const fn new(
addr: Ipv4Addr,
prefix: u8,
) -> Result<Ipv4Network, IpNetworkError>
pub const fn new( addr: Ipv4Addr, prefix: u8, ) -> Result<Ipv4Network, IpNetworkError>
Constructs a new Ipv4Network from any Ipv4Addr and a prefix denoting the network size.
If the prefix is larger than 32 this will return an IpNetworkError::InvalidPrefix.
Sourcepub const fn new_checked(addr: Ipv4Addr, prefix: u8) -> Option<Ipv4Network>
pub const fn new_checked(addr: Ipv4Addr, prefix: u8) -> Option<Ipv4Network>
Constructs a new Ipv4Network from any Ipv4Addr, and a prefix denoting the network size.
If the prefix is larger than 32 this will return None. This is useful in const contexts,
where Option::unwrap may be called to trigger a compile-time error in case the prefix
is an unexpected value.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
const PREFIX: u8 = 24;
const ADDR: Ipv4Addr = Ipv4Addr::new(192, 168, 1, 1);
// Okay!
const NETWORK: Ipv4Network = Ipv4Network::new_checked(ADDR, PREFIX).unwrap();
assert_eq!(NETWORK.prefix(), PREFIX);use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
// Prefix is greater than 32.
const PREFIX: u8 = 32 + 1;
const ADDR: Ipv4Addr = Ipv4Addr::new(192, 168, 1, 1);
// This fails!
const NETWORK: Option<Ipv4Network> = Ipv4Network::new_checked(ADDR, PREFIX);
assert_eq!(NETWORK.unwrap().prefix(), PREFIX);Sourcepub fn with_netmask(
netaddr: Ipv4Addr,
netmask: Ipv4Addr,
) -> Result<Ipv4Network, IpNetworkError>
pub fn with_netmask( netaddr: Ipv4Addr, netmask: Ipv4Addr, ) -> Result<Ipv4Network, IpNetworkError>
Constructs a new Ipv4Network from a network address and a network mask.
If the netmask is not valid this will return an IpNetworkError::InvalidPrefix.
Sourcepub fn iter(self) -> Ipv4NetworkIterator
pub fn iter(self) -> Ipv4NetworkIterator
Returns an iterator over Ipv4Network. Each call to next will return the next
Ipv4Addr in the given network. None will be returned when there are no more
addresses.
pub fn ip(self) -> Ipv4Addr
pub fn prefix(self) -> u8
Sourcepub fn is_subnet_of(self, other: Ipv4Network) -> bool
pub fn is_subnet_of(self, other: Ipv4Network) -> bool
Checks if the given Ipv4Network is a subnet of the other.
Sourcepub fn is_supernet_of(self, other: Ipv4Network) -> bool
pub fn is_supernet_of(self, other: Ipv4Network) -> bool
Checks if the given Ipv4Network is a supernet of the other.
Sourcepub fn overlaps(self, other: Ipv4Network) -> bool
pub fn overlaps(self, other: Ipv4Network) -> bool
Checks if the given Ipv4Network is partly contained in other.
Sourcepub fn mask(&self) -> Ipv4Addr
pub fn mask(&self) -> Ipv4Addr
Returns the mask for this Ipv4Network.
That means the prefix most significant bits will be 1 and the rest 0
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "127.0.0.0".parse().unwrap();
assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 255, 255));
let net: Ipv4Network = "127.0.0.0/16".parse().unwrap();
assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 0, 0));Sourcepub fn network(&self) -> Ipv4Addr
pub fn network(&self) -> Ipv4Addr
Returns the address of the network denoted by this Ipv4Network.
This means the lowest possible IPv4 address inside of the network.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "10.1.9.32/16".parse().unwrap();
assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));Sourcepub fn broadcast(&self) -> Ipv4Addr
pub fn broadcast(&self) -> Ipv4Addr
Returns the broadcasting address of this Ipv4Network.
This means the highest possible IPv4 address inside of the network.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));Sourcepub fn contains(&self, ip: Ipv4Addr) -> bool
pub fn contains(&self, ip: Ipv4Addr) -> bool
Checks if a given Ipv4Addr is in this Ipv4Network
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "127.0.0.0/24".parse().unwrap();
assert!(net.contains(Ipv4Addr::new(127, 0, 0, 70)));
assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));Sourcepub fn size(self) -> u32
pub fn size(self) -> u32
Returns number of possible host addresses in this Ipv4Network.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "10.1.0.0/16".parse().unwrap();
assert_eq!(net.size(), 65536);
let tinynet: Ipv4Network = "0.0.0.0/32".parse().unwrap();
assert_eq!(tinynet.size(), 1);Sourcepub fn nth(self, n: u32) -> Option<Ipv4Addr>
pub fn nth(self, n: u32) -> Option<Ipv4Addr>
Returns the n:th address within this network.
The adresses are indexed from 0 and n must be smaller than the size of the network.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let net: Ipv4Network = "192.168.0.0/24".parse().unwrap();
assert_eq!(net.nth(0).unwrap(), Ipv4Addr::new(192, 168, 0, 0));
assert_eq!(net.nth(15).unwrap(), Ipv4Addr::new(192, 168, 0, 15));
assert!(net.nth(256).is_none());
let net2: Ipv4Network = "10.0.0.0/16".parse().unwrap();
assert_eq!(net2.nth(256).unwrap(), Ipv4Addr::new(10, 0, 1, 0));Trait Implementationsยง
Sourceยงimpl Clone for Ipv4Network
impl Clone for Ipv4Network
Sourceยงfn clone(&self) -> Ipv4Network
fn clone(&self) -> Ipv4Network
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSourceยงimpl Debug for Ipv4Network
impl Debug for Ipv4Network
Sourceยงimpl Display for Ipv4Network
impl Display for Ipv4Network
Sourceยงimpl From<Ipv4Addr> for Ipv4Network
impl From<Ipv4Addr> for Ipv4Network
Sourceยงfn from(a: Ipv4Addr) -> Ipv4Network
fn from(a: Ipv4Addr) -> Ipv4Network
Sourceยงimpl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
Sourceยงfn from(v4: Ipv4Network) -> IpNetwork
fn from(v4: Ipv4Network) -> IpNetwork
Sourceยงimpl FromStr for Ipv4Network
Creates an Ipv4Network from parsing a string in CIDR notation.
impl FromStr for Ipv4Network
Creates an Ipv4Network from parsing a string in CIDR notation.
ยงExamples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;
let new = Ipv4Network::new(Ipv4Addr::new(10, 1, 9, 32), 16).unwrap();
let from_cidr: Ipv4Network = "10.1.9.32/16".parse().unwrap();
assert_eq!(new.ip(), from_cidr.ip());
assert_eq!(new.prefix(), from_cidr.prefix());Sourceยงtype Err = IpNetworkError
type Err = IpNetworkError
Sourceยงfn from_str(s: &str) -> Result<Ipv4Network, <Ipv4Network as FromStr>::Err>
fn from_str(s: &str) -> Result<Ipv4Network, <Ipv4Network as FromStr>::Err>
s to return a value of this type. Read moreSourceยงimpl Hash for Ipv4Network
impl Hash for Ipv4Network
Sourceยงimpl IntoIterator for &Ipv4Network
impl IntoIterator for &Ipv4Network
Sourceยงimpl Ord for Ipv4Network
impl Ord for Ipv4Network
Sourceยงfn cmp(&self, other: &Ipv4Network) -> Ordering
fn cmp(&self, other: &Ipv4Network) -> Ordering
1.21.0 ยท Sourceยงfn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงimpl PartialEq for Ipv4Network
impl PartialEq for Ipv4Network
Sourceยงimpl PartialOrd for Ipv4Network
impl PartialOrd for Ipv4Network
Sourceยงimpl TryFrom<&str> for Ipv4Network
impl TryFrom<&str> for Ipv4Network
Sourceยงtype Error = IpNetworkError
type Error = IpNetworkError
Sourceยงfn try_from(
s: &str,
) -> Result<Ipv4Network, <Ipv4Network as TryFrom<&str>>::Error>
fn try_from( s: &str, ) -> Result<Ipv4Network, <Ipv4Network as TryFrom<&str>>::Error>
impl Copy for Ipv4Network
impl Eq for Ipv4Network
impl StructuralPartialEq for Ipv4Network
Auto Trait Implementationsยง
impl Freeze for Ipv4Network
impl RefUnwindSafe for Ipv4Network
impl Send for Ipv4Network
impl Sync for Ipv4Network
impl Unpin for Ipv4Network
impl UnwindSafe for Ipv4Network
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Sourceยงfn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self> โ
fn into_either(self, into_left: bool) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more