Struct ipnetwork::Ipv4Network
source · [−]pub struct Ipv4Network { /* private fields */ }
Expand description
Represents a network range where the IP addresses are of v4
Implementations
sourceimpl Ipv4Network
impl Ipv4Network
sourcepub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError>
pub 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
.
sourcepub fn iter(&self) -> Ipv4NetworkIterator
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.
pub fn ip(&self) -> Ipv4Addr
pub fn prefix(&self) -> u8
sourcepub fn mask(&self) -> Ipv4Addr
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/16".parse().unwrap();
assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 0, 0));
sourcepub fn network(&self) -> Ipv4Addr
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));
sourcepub fn broadcast(&self) -> Ipv4Addr
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));
sourcepub fn contains(&self, ip: Ipv4Addr) -> bool
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)));
sourcepub fn size(&self) -> u64
pub fn size(&self) -> u64
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);
sourcepub fn nth(&self, n: u32) -> Option<Ipv4Addr>
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
sourceimpl Clone for Ipv4Network
impl Clone for Ipv4Network
sourcefn clone(&self) -> Ipv4Network
fn clone(&self) -> Ipv4Network
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Ipv4Network
impl Debug for Ipv4Network
sourceimpl Display for Ipv4Network
impl Display for Ipv4Network
sourceimpl From<Ipv4Addr> for Ipv4Network
impl From<Ipv4Addr> for Ipv4Network
sourcefn from(a: Ipv4Addr) -> Ipv4Network
fn from(a: Ipv4Addr) -> Ipv4Network
Performs the conversion.
sourceimpl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
sourcefn from(v4: Ipv4Network) -> IpNetwork
fn from(v4: Ipv4Network) -> IpNetwork
Performs the conversion.
sourceimpl FromStr for Ipv4Network
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());
type Err = IpNetworkError
type Err = IpNetworkError
The associated error which can be returned from parsing.
sourcefn from_str(s: &str) -> Result<Ipv4Network, IpNetworkError>
fn from_str(s: &str) -> Result<Ipv4Network, IpNetworkError>
Parses a string s
to return a value of this type. Read more
sourceimpl Hash for Ipv4Network
impl Hash for Ipv4Network
sourceimpl Ord for Ipv4Network
impl Ord for Ipv4Network
sourceimpl PartialEq<Ipv4Network> for Ipv4Network
impl PartialEq<Ipv4Network> for Ipv4Network
sourcefn eq(&self, other: &Ipv4Network) -> bool
fn eq(&self, other: &Ipv4Network) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &Ipv4Network) -> bool
fn ne(&self, other: &Ipv4Network) -> bool
This method tests for !=
.
sourceimpl PartialOrd<Ipv4Network> for Ipv4Network
impl PartialOrd<Ipv4Network> for Ipv4Network
sourcefn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for Ipv4Network
impl Eq for Ipv4Network
impl StructuralEq for Ipv4Network
impl StructuralPartialEq for Ipv4Network
Auto Trait Implementations
impl RefUnwindSafe for Ipv4Network
impl Send for Ipv4Network
impl Sync for Ipv4Network
impl Unpin for Ipv4Network
impl UnwindSafe for Ipv4Network
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more