pub struct Cidr { /* private fields */ }Available on crate feature
net only.Expand description
A CIDR (Classless Inter-Domain Routing) notation for IP networks.
This type represents an IP network prefix in the form address/prefix_length,
where address is an IP address and prefix_length is the number of
leading bits that represent the network portion.
§Examples
use bare_types::net::{Cidr, IpAddr};
// Create CIDR from string
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
// Check if IP is in network
let check_ip: IpAddr = "192.168.1.100".parse().unwrap();
assert!(cidr.contains(&check_ip));
// Get network address
let network = cidr.network_address();
// Get broadcast address (only for IPv4)
let broadcast = cidr.broadcast_address();
// Create from IP address and prefix length
let ip: IpAddr = "10.0.0.0".parse().unwrap();
let cidr2 = Cidr::new(ip, 8).unwrap();Implementations§
Source§impl Cidr
impl Cidr
Sourcepub const fn address(&self) -> IpAddr
pub const fn address(&self) -> IpAddr
Returns the IP address.
§Examples
use bare_types::net::{Cidr, IpAddr};
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
let ip = cidr.address();Sourcepub const fn prefix_length(&self) -> u8
pub const fn prefix_length(&self) -> u8
Returns the prefix length.
§Examples
use bare_types::net::{Cidr, IpAddr};
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
assert_eq!(cidr.prefix_length(), 24);Sourcepub fn network_address(&self) -> IpAddr
pub fn network_address(&self) -> IpAddr
Returns the network address.
The network address is the IP address with all host bits set to zero.
§Examples
use bare_types::net::Cidr;
let cidr: Cidr = "192.168.1.100/24".parse().unwrap();
let network = cidr.network_address();
assert_eq!(network.to_string(), "192.168.1.0");Sourcepub fn broadcast_address(&self) -> Option<IpAddr>
pub fn broadcast_address(&self) -> Option<IpAddr>
Returns the broadcast address.
The broadcast address is the IP address with all host bits set to one. Only applicable for IPv4 networks.
§Examples
use bare_types::net::Cidr;
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
let broadcast = cidr.broadcast_address().unwrap();
assert_eq!(broadcast.to_string(), "192.168.1.255");Sourcepub fn contains(&self, ip: &IpAddr) -> bool
pub fn contains(&self, ip: &IpAddr) -> bool
Checks if an IP address is contained in this CIDR network.
§Examples
use bare_types::net::{Cidr, IpAddr};
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
let ip1: IpAddr = "192.168.1.100".parse().unwrap();
let ip2: IpAddr = "192.168.2.100".parse().unwrap();
assert!(cidr.contains(&ip1));
assert!(!cidr.contains(&ip2));Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Cidr
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Cidr
Available on crate feature
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Generate an arbitrary value of
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Generate an arbitrary value of
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§impl<'de> Deserialize<'de> for Cidr
impl<'de> Deserialize<'de> for Cidr
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
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 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