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.
Sourcepub fn first(&self) -> Option<IpAddr>
pub fn first(&self) -> Option<IpAddr>
Return the first IP address which falls within this network.
For networks with no distinct network address (/32 for IPv4 and
/128 for IPv6, which contain only a single host; and /31 for IPv4
and /127 for IPv6, where per RFC 3021 / RFC 6164 both addresses are
usable), self.ip is returned. Otherwise the network address is
skipped and self.ip + 1 is returned.
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>
impl Copy for IpNet
impl Eq for IpNet
impl StructuralPartialEq for IpNet
Auto Trait Implementations§
impl Freeze for IpNet
impl RefUnwindSafe for IpNet
impl Send for IpNet
impl Sync for IpNet
impl Unpin for IpNet
impl UnsafeUnpin 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.