pub struct Cidr { /* private fields */ }Expand description
An IP network in CIDR notation: a base address plus a prefix length.
Cidr validates that the prefix length is in range for the address family
(0..=32 for IPv4, 0..=128 for IPv6) at construction. It accepts both IPv4
and IPv6 via core::net::IpAddr, stores the address exactly as supplied
(host bits are not cleared), and offers membership testing with
contains and the canonical network base with
network.
This type is allocation-free and no_std-friendly.
§Examples
use reliakit_primitives::Cidr;
let net: Cidr = "192.168.1.0/24".parse().unwrap();
assert!(net.contains("192.168.1.42".parse().unwrap()));
assert!(!net.contains("192.168.2.1".parse().unwrap()));
assert_eq!(net.prefix_len(), 24);Implementations§
Source§impl Cidr
impl Cidr
Sourcepub fn new(addr: IpAddr, prefix_len: u8) -> PrimitiveResult<Self>
pub fn new(addr: IpAddr, prefix_len: u8) -> PrimitiveResult<Self>
Creates a Cidr from an address and prefix length.
Returns PrimitiveError::Invalid if prefix_len exceeds the maximum
for the address family (32 for IPv4, 128 for IPv6).
Sourcepub const fn address(&self) -> IpAddr
pub const fn address(&self) -> IpAddr
Returns the base address exactly as supplied (host bits not cleared).
Sourcepub const fn prefix_len(&self) -> u8
pub const fn prefix_len(&self) -> u8
Returns the prefix length (number of leading network bits).
Trait Implementations§
impl Copy for Cidr
impl Eq for Cidr
impl StructuralPartialEq for Cidr
Auto Trait Implementations§
impl Freeze for Cidr
impl RefUnwindSafe for Cidr
impl Send for Cidr
impl Sync for Cidr
impl Unpin for Cidr
impl UnsafeUnpin for Cidr
impl UnwindSafe for Cidr
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
Mutably borrows from an owned value. Read more