Struct MaskedIpAddr

Source
pub struct MaskedIpAddr { /* private fields */ }
Expand description

An IP address, if necessary in CIDR notation.

Implementations§

Source§

impl MaskedIpAddr

Source

pub fn new<I: Into<IpAddr>>(addr: I, cidr: u8) -> MaskedIpAddr

Creates a new MaskedIpAddr from components.

Do not pass an addr with bits set to the right of the netmask if you intend to insert this into a postgres cidr field.

§Panics

Panics if the CIDR is greater than 32 for an IPv4 address, or is greater than 128 for an IPv6 address.

§Examples

To represent an address:

let ip = Ipv4Addr::new(192, 0, 2, 142);
MaskedIpAddr::new(ip, 32);

To represent a network:

let network = Ipv6Addr::new(0x2001, 0x0DB8, 0, 0, 0, 0, 0, 0);
MaskedIpAddr::new(network, 32);
Source

pub fn is_unspecified(&self) -> bool

Returns true for the special ‘unspecified’ address.

See the documentation for Ipv4Addr::is_unspecified and Ipv6Addr::is_unspecified for more details.

§Examples
assert!(MaskedIpAddr::new(Ipv4Addr::new(0, 0, 0, 0), 32).is_unspecified());
assert!(MaskedIpAddr::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 128).is_unspecified());
Source

pub fn is_loopback(&self) -> bool

Returns true if this is a loopback address.

See the documentation for Ipv4Addr::is_loopback and Ipv6Addr::is_loopback for more details.

§Examples
assert!(MaskedIpAddr::new(Ipv4Addr::new(127, 0, 0, 1), 32).is_loopback());
assert!(MaskedIpAddr::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 128).is_loopback());
Source

pub fn is_multicast(&self) -> bool

Returns true if this is a multicast address.

See the documentation for Ipv4Addr::is_multicast and Ipv6Addr::is_multicast for more details.

§Examples
assert!(MaskedIpAddr::new(Ipv4Addr::new(224, 254, 0, 0), 32).is_multicast());
assert!(MaskedIpAddr::new(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0), 128).is_multicast());
Source

pub fn is_ipv4(&self) -> bool

Returns true if this address is an IPv4 address, and false otherwise.

§Examples
assert!(MaskedIpAddr::new(Ipv4Addr::new(203, 0, 113, 6), 32).is_ipv4());
assert!(!MaskedIpAddr::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 128).is_ipv4());
Source

pub fn is_ipv6(&self) -> bool

Returns true if this address is an IPv6 address, and false otherwise.

§Examples
assert!(!MaskedIpAddr::new(Ipv4Addr::new(203, 0, 113, 6), 32).is_ipv6());
assert!(MaskedIpAddr::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 128).is_ipv6());
Source

pub fn address(&self) -> IpAddr

Returns the contained IP address.

§Examples
let ip = Ipv4Addr::new(192, 0, 2, 142);
assert_eq!(MaskedIpAddr::new(ip, 32).address(), ip);
let network = Ipv6Addr::new(0x2001, 0x0DB8, 0, 0, 0, 0, 0, 0);
assert_eq!(MaskedIpAddr::new(network, 32).address(), network);
Source

pub fn cidr(&self) -> u8

Returns the contained CIDR.

§Examples
assert_eq!(MaskedIpAddr::new(Ipv4Addr::new(192, 0, 2, 142), 32).cidr(), 32);
assert_eq!(MaskedIpAddr::new(Ipv6Addr::new(0x2001, 0x0DB8, 0, 0, 0, 0, 0, 0), 64).cidr(), 64);
Source

pub fn netmask(&self) -> u8

👎Deprecated since 0.15.2: Supported because of historical (and wrong) use of netmask instead of CIDR. Use MaskedIpAddr::cidr instead.

Returns the contained CIDR.

§Examples
assert_eq!(MaskedIpAddr::new(Ipv4Addr::new(192, 0, 2, 142), 32).netmask(), 32);
assert_eq!(MaskedIpAddr::new(Ipv6Addr::new(0x2001, 0x0DB8, 0, 0, 0, 0, 0, 0), 64).netmask(), 64);
Source

pub fn subnet_mask(&self) -> u128

Returns the subnet mask, calculated from the CIDR.

Source

pub fn into_inner(self) -> (IpAddr, u8)

Consumes the MaskedIpAddr, returning the IP address and netmask.

§Examples
let network = Ipv4Addr::new(198, 51, 100, 0);
assert_eq!(MaskedIpAddr::new(network, 24).into_inner(), (network.into(), 24));

Trait Implementations§

Source§

impl Clone for MaskedIpAddr

Source§

fn clone(&self) -> MaskedIpAddr

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 MaskedIpAddr

Source§

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

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

impl Display for MaskedIpAddr

Source§

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

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

impl From<[u16; 8]> for MaskedIpAddr

Source§

fn from(segments: [u16; 8]) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<[u8; 16]> for MaskedIpAddr

Source§

fn from(octets: [u8; 16]) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<[u8; 4]> for MaskedIpAddr

Source§

fn from(octets: [u8; 4]) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<IpAddr> for MaskedIpAddr

Source§

fn from(ip: IpAddr) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<Ipv4Addr> for MaskedIpAddr

Source§

fn from(ipv4: Ipv4Addr) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<Ipv6Addr> for MaskedIpAddr

Source§

fn from(ipv6: Ipv6Addr) -> MaskedIpAddr

Converts to this type from the input type.
Source§

impl From<MaskedIpAddr> for IpAddr

Source§

fn from(mip: MaskedIpAddr) -> IpAddr

Converts to this type from the input type.
Source§

impl FromSql<'_> for MaskedIpAddr

Source§

fn from_sql( _: &Type, raw: &[u8], ) -> Result<Self, Box<dyn Error + Sync + Send + 'static>>

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.
Source§

fn from_sql_null(ty: &Type) -> Result<Self, Box<dyn Error + Sync + Send>>

Creates a new value of this type from a NULL SQL value. Read more
Source§

fn from_sql_nullable( ty: &Type, raw: Option<&'a [u8]>, ) -> Result<Self, Box<dyn Error + Sync + Send>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw.
Source§

impl FromStr for MaskedIpAddr

Source§

type Err = MaskedIpAddrParseError

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

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

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

impl Hash for MaskedIpAddr

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 Ord for MaskedIpAddr

Source§

fn cmp(&self, other: &MaskedIpAddr) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for MaskedIpAddr

Source§

fn eq(&self, other: &MaskedIpAddr) -> 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 PartialOrd for MaskedIpAddr

Source§

fn partial_cmp(&self, other: &MaskedIpAddr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToSql for MaskedIpAddr

Source§

fn to_sql( &self, ty: &Type, w: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be converted to the specified Postgres Type.
Source§

fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

An adaptor method used internally by Rust-Postgres. Read more
Source§

impl Copy for MaskedIpAddr

Source§

impl Eq for MaskedIpAddr

Source§

impl StructuralPartialEq for MaskedIpAddr

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> BorrowToSql for T
where T: ToSql,

Source§

fn borrow_to_sql(&self) -> &dyn ToSql

Returns a reference to self as a ToSql trait object.
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> FromSqlOwned for T
where T: for<'a> FromSql<'a>,