ipnetwork

Struct Ipv4Network

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

Represents a network range where the IP addresses are of v4

Implementations§

Source§

impl Ipv4Network

Source

pub const fn new( addr: Ipv4Addr, prefix: u8, ) -> Result<Ipv4Network, IpNetworkError>

Constructs a new Ipv4Network from any Ipv4Addr and a prefix denoting the network size.

If the prefix is larger than 32 this will return an IpNetworkError::InvalidPrefix.

Source

pub fn with_netmask( netaddr: Ipv4Addr, netmask: Ipv4Addr, ) -> Result<Ipv4Network, IpNetworkError>

Constructs a new Ipv4Network from a network address and a network mask.

If the netmask is not valid this will return an IpNetworkError::InvalidPrefix.

Source

pub fn iter(self) -> Ipv4NetworkIterator

Returns an iterator over Ipv4Network. Each call to next will return the next Ipv4Addr in the given network. None will be returned when there are no more addresses.

Source

pub fn ip(self) -> Ipv4Addr

Source

pub fn prefix(self) -> u8

Source

pub fn is_subnet_of(self, other: Ipv4Network) -> bool

Checks if the given Ipv4Network is a subnet of the other.

Source

pub fn is_supernet_of(self, other: Ipv4Network) -> bool

Checks if the given Ipv4Network is a supernet of the other.

Source

pub fn overlaps(self, other: Ipv4Network) -> bool

Checks if the given Ipv4Network is partly contained in other.

Source

pub fn mask(self) -> Ipv4Addr

Returns the mask for this Ipv4Network. That means the prefix most significant bits will be 1 and the rest 0

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "127.0.0.0".parse().unwrap();
assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 255, 255));
let net: Ipv4Network = "127.0.0.0/16".parse().unwrap();
assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 0, 0));
Source

pub fn network(self) -> Ipv4Addr

Returns the address of the network denoted by this Ipv4Network. This means the lowest possible IPv4 address inside of the network.

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "10.1.9.32/16".parse().unwrap();
assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));
Source

pub fn broadcast(self) -> Ipv4Addr

Returns the broadcasting address of this Ipv4Network. This means the highest possible IPv4 address inside of the network.

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));
Source

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

Checks if a given Ipv4Addr is in this Ipv4Network

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "127.0.0.0/24".parse().unwrap();
assert!(net.contains(Ipv4Addr::new(127, 0, 0, 70)));
assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));
Source

pub fn size(self) -> u32

Returns number of possible host addresses in this Ipv4Network.

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "10.1.0.0/16".parse().unwrap();
assert_eq!(net.size(), 65536);

let tinynet: Ipv4Network = "0.0.0.0/32".parse().unwrap();
assert_eq!(tinynet.size(), 1);
Source

pub fn nth(self, n: u32) -> Option<Ipv4Addr>

Returns the n:th address within this network. The adresses are indexed from 0 and n must be smaller than the size of the network.

§Examples
use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let net: Ipv4Network = "192.168.0.0/24".parse().unwrap();
assert_eq!(net.nth(0).unwrap(), Ipv4Addr::new(192, 168, 0, 0));
assert_eq!(net.nth(15).unwrap(), Ipv4Addr::new(192, 168, 0, 15));
assert!(net.nth(256).is_none());

let net2: Ipv4Network = "10.0.0.0/16".parse().unwrap();
assert_eq!(net2.nth(256).unwrap(), Ipv4Addr::new(10, 0, 1, 0));

Trait Implementations§

Source§

impl Clone for Ipv4Network

Source§

fn clone(&self) -> Ipv4Network

Returns a copy 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 Ipv4Network

Source§

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

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

impl<'de> Deserialize<'de> for Ipv4Network

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Ipv4Network

Source§

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

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

impl From<Ipv4Addr> for Ipv4Network

Source§

fn from(a: Ipv4Addr) -> Ipv4Network

Converts to this type from the input type.
Source§

impl From<Ipv4Network> for IpNetwork

Source§

fn from(v4: Ipv4Network) -> IpNetwork

Converts to this type from the input type.
Source§

impl FromStr for Ipv4Network

Creates an Ipv4Network from parsing a string in CIDR notation.

§Examples

use std::net::Ipv4Addr;
use ipnetwork::Ipv4Network;

let new = Ipv4Network::new(Ipv4Addr::new(10, 1, 9, 32), 16).unwrap();
let from_cidr: Ipv4Network = "10.1.9.32/16".parse().unwrap();
assert_eq!(new.ip(), from_cidr.ip());
assert_eq!(new.prefix(), from_cidr.prefix());
Source§

type Err = IpNetworkError

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 Ipv4Network

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 IntoIterator for &Ipv4Network

Source§

type IntoIter = Ipv4NetworkIterator

Which kind of iterator are we turning this into?
Source§

type Item = Ipv4Addr

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Ipv4NetworkIterator

Creates an iterator from a value. Read more
Source§

impl Ord for Ipv4Network

Source§

fn cmp(&self, other: &Ipv4Network) -> 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 Ipv4Network

Source§

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

Source§

fn partial_cmp(&self, other: &Ipv4Network) -> 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 Serialize for Ipv4Network

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&str> for Ipv4Network

Source§

type Error = IpNetworkError

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

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Ipv4Network

Source§

impl Eq for Ipv4Network

Source§

impl StructuralPartialEq for Ipv4Network

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,