Struct MaskedIpv4

Source
pub struct MaskedIpv4 {
    pub ip: Ipv4Addr,
    pub mask: Ipv4Mask,
}
Expand description

An 8-byte type representing an IPv4 address and subnet mask pair. The IP may be any ip within the represented network, and the mask may be any valid subnet mask.

Fields§

§ip: Ipv4Addr

The IP address

§mask: Ipv4Mask

The subnet mask

Implementations§

Source§

impl MaskedIpv4

Source

pub const fn new(ip: Ipv4Addr, mask: Ipv4Mask) -> Self

Constructs a MaskedIpv4 from the provided ip and mask.

Source

pub const fn cidr(ip: Ipv4Addr, mask_len: u8) -> Self

Constructs a MaskedIpv4 from the provided ip and mask length.

§Panics

Will panic if provided length > 32

Source

pub fn from_cidr_str(s: &str) -> Option<Self>

Constructs a new MaskedIpv4 from the provided CIDR string.

Source

pub fn from_network_str(s: &str) -> Option<Self>

Constructs a new MaskedIpv4 from the provided IP and subnet mask. There must be exactly one space between the IP and mask.

Source

pub fn to_cidr_string(&self) -> String

Returns a String with the IP and mask in CIDR format. Shortcut for format!("{:#}", self)

Source

pub fn to_network_string(&self) -> String

Returns a String with the IP and mask in dotted decimal format. Shortcut for format!("{}", self)

Source

pub fn network_address(&self) -> Ipv4Addr

Returns the network adderss by setting all host bits to 0.

Source

pub fn network(&self) -> MaskedIpv4

Constructs a new MaskedIpv4 using the network address and mask of this MaskedIpv4.

Source

pub fn is_network_address(&self) -> bool

Returns true if all host bits in the IP are 0. Always returns false if the mask length is 31 or 32.

Source

pub fn broadcast_address(&self) -> Ipv4Addr

Returns the broadcast address by setting all host bits to 1.

Source

pub fn is_broadcast_address(&self) -> bool

Returns true if all host bits in the IP are 1. Always returns false if the mask length is 31 or 32.

Source

pub fn network_bits(&self) -> u8

Returns the number of network bits. That is, the length of the mask.

Source

pub fn host_bits(&self) -> u8

Returns the number of host bits. That is, the number of 0 bits in the mask.

Source

pub fn host_count(&self) -> usize

Returns the number of host addresses in the network.

§Panics

Will panic if usize is not large enough to hold the host count.

Source

pub fn host_count_u64(&self) -> u64

Returns the number of host addresses in the network as u64. Unlike host_count, this will never panic.

Source

pub fn network_count(&self, len: u8) -> usize

Returns the number of networks of the provided mask length will fit in this network.

§Panics

Will panic if the provided length is > 32, or if the number of networks does not fit in usize

Source

pub fn network_count_u64(&self, len: u8) -> u64

Returns the number of networks of the provided mask length will fit in this network as u64. Unlike network_count, this will not panic due to overflow. May still panic if the provided length is too long.

§Panics

Will panic if the provided length is > 32

Source

pub fn contains(&self, ip: Ipv4Addr) -> bool

Returns true if this network contains the provided IP address, even if the provided IP is the network or broadcast address.

Trait Implementations§

Source§

impl Clone for MaskedIpv4

Source§

fn clone(&self) -> MaskedIpv4

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MaskedIpv4

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl Display for MaskedIpv4

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl FromStr for MaskedIpv4

Source§

type Err = InvalidMaskedIpv4

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, InvalidMaskedIpv4>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for MaskedIpv4

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for MaskedIpv4

Source§

fn eq(&self, other: &MaskedIpv4) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for MaskedIpv4

Source§

impl Eq for MaskedIpv4

Source§

impl StructuralPartialEq for MaskedIpv4

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.