pub struct IpNet { /* private fields */ }
Expand description
An IpNet represents an IP network. Networks are typically identified in CIDR notation, like (for example) “192.0.0.0/24”.
Implementations§
source§impl IpNet
impl IpNet
sourcepub fn get_ip(&self) -> IpAddr
pub fn get_ip(&self) -> IpAddr
Return the network IP. This address will have been normalized, such that any non-masked bits will have been turned off during construction.
sourcepub fn get_mask(&self) -> &[u8] ⓘ
pub fn get_mask(&self) -> &[u8] ⓘ
Return the network mask, as a byte slice. This slice will always be 16 bytes long, even if this is an IPv4 network.
sourcepub fn apply_mask(&self, ip: IpAddr, invert: bool, set: bool) -> IpAddr
pub fn apply_mask(&self, ip: IpAddr, invert: bool, set: bool) -> IpAddr
Apply this network’s mask to the given IP address, returning the modified copy.
The “default” behavior is when invert=false and set=false. In this case, any bits which are “0” in the mask are turned off in the given IP address’ bytes.
If invert=true, then each bit in the mask is flipped before it is applied - in other words, we apply the inverse mask.
If set=true, then we switch from bitwise AND to bitwise OR, meaning instead of the behavior described above, any bits which are “1” in the mask are turned on in the IP address, and other bits are left unchanged.
sourcepub fn get_one_bits(&self) -> usize
pub fn get_one_bits(&self) -> usize
Returns the number of “1” bits in this network’s mask. Although the mask is always 16 bytes long, for IPv4 networks only the last 4 bytes of the mask are considered.
sourcepub fn is_canonical(&self) -> bool
pub fn is_canonical(&self) -> bool
Returns whether or not this network’s mask is “canonical” - i.e., if all of its mask’s “1” bits are contiguous (no “0” bits in-between them).
sourcepub fn contains(&self, ip: IpAddr, strict: bool) -> bool
pub fn contains(&self, ip: IpAddr, strict: bool) -> bool
Return whether or not the given IP address is contained within this network. If strict=true, then the “network address” and “broadcast address” are both not considered to be “within” this network (this is useful if you want to check if the given IP address is actually usable for a host on the network).
sourcepub fn increment_in(&self, ip: IpAddr, strict: bool) -> Option<IpAddr>
pub fn increment_in(&self, ip: IpAddr, strict: bool) -> Option<IpAddr>
Increment the given IP address as if with increment_ip(), but returning None if the resulting IP address does not lie within this network. The “strict” parameter behaves as IpNet::contains() describes.
Trait Implementations§
source§impl<'de> Deserialize<'de> for IpNet
impl<'de> Deserialize<'de> for IpNet
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl PartialEq<IpNet> for IpNet
impl PartialEq<IpNet> for IpNet
impl Copy for IpNet
impl Eq for IpNet
impl StructuralEq for IpNet
impl StructuralPartialEq for IpNet
Auto Trait Implementations§
impl RefUnwindSafe for IpNet
impl Send for IpNet
impl Sync for IpNet
impl Unpin for IpNet
impl UnwindSafe for IpNet
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<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.