pub struct Cidr { /* private fields */ }Expand description
A network block: a normalized network address and a prefix length.
§Examples
use net_io::Cidr;
let office: Cidr = "203.0.113.0/24".parse()?;
assert!(office.contains("203.0.113.9".parse().unwrap()));
assert!(!office.contains("198.51.100.1".parse().unwrap()));
assert_eq!(office.to_string(), "203.0.113.0/24");
assert!(Cidr::parse("10.0.0.1/8").is_err());
assert_eq!(Cidr::new_masked("10.0.0.1".parse().unwrap(), 8)?.to_string(), "10.0.0.0/8");Implementations§
Source§impl Cidr
impl Cidr
Sourcepub fn new(ip: IpAddr, prefix: u8) -> Result<Cidr, AddrError>
pub fn new(ip: IpAddr, prefix: u8) -> Result<Cidr, AddrError>
A block whose host bits must be zero.
§Errors
AddrError::PrefixTooLong, AddrError::HostBitsSet.
let block = net_io::Cidr::new("10.0.0.0".parse().unwrap(), 8)?;
assert_eq!(block.prefix_len(), 8);Sourcepub fn new_masked(ip: IpAddr, prefix: u8) -> Result<Cidr, AddrError>
pub fn new_masked(ip: IpAddr, prefix: u8) -> Result<Cidr, AddrError>
A block that clears host bits instead of refusing them.
§Errors
let block = net_io::Cidr::new_masked("192.0.2.77".parse().unwrap(), 24)?;
assert_eq!(block.to_string(), "192.0.2.0/24");Sourcepub fn parse(text: &str) -> Result<Cidr, AddrError>
pub fn parse(text: &str) -> Result<Cidr, AddrError>
Parses "10.0.0.0/8", "2001:db8::/32", or a bare address (/32 or /128).
Canonical text only: no leading zeros in IPv4 octets, no zone ids, no whitespace.
§Errors
AddrError::Empty, AddrError::Syntax, AddrError::PrefixTooLong,
AddrError::HostBitsSet.
let block = net_io::Cidr::parse("::1")?;
assert_eq!(block.prefix_len(), 128);Sourcepub fn contains(&self, ip: IpAddr) -> bool
pub fn contains(&self, ip: IpAddr) -> bool
Whether the block contains ip (normalized first).
let block = net_io::Cidr::parse("10.0.0.0/8")?;
assert!(block.contains("::ffff:10.1.2.3".parse().unwrap()));Sourcepub fn network(&self) -> IpAddr
pub fn network(&self) -> IpAddr
The network address.
let block = net_io::Cidr::parse("10.0.0.0/8")?;
assert_eq!(block.network().to_string(), "10.0.0.0");Sourcepub fn prefix_len(&self) -> u8
pub fn prefix_len(&self) -> u8
The prefix length.
assert_eq!(net_io::Cidr::parse("10.0.0.0/8")?.prefix_len(), 8);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