Skip to main content

Ipv6Network

Struct Ipv6Network 

Source
pub struct Ipv6Network { /* private fields */ }
Available on crate feature ipnetwork only.
Expand description

Represents a network range where the IP addresses are of v6

Implementationsยง

Sourceยง

impl Ipv6Network

Source

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

Constructs a new Ipv6Network from any Ipv6Addr and a prefix denoting the network size.

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

Source

pub const fn new_checked(addr: Ipv6Addr, prefix: u8) -> Option<Ipv6Network>

Constructs a new Ipv6Network from any Ipv6Addr, and a prefix denoting the network size.

If the prefix is larger than 128 this will return None. This is useful in const contexts, where Option::unwrap may be called to trigger a compile-time error in case the prefix is an unexpected value.

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

const PREFIX: u8 = 64;
const ADDR: Ipv6Addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);

// Okay!
const NETWORK: Ipv6Network = Ipv6Network::new_checked(ADDR, PREFIX).unwrap();
assert_eq!(NETWORK.prefix(), PREFIX);
โ“˜
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

// Prefix is greater than 128.
const PREFIX: u8 = 128 + 1;
const ADDR: Ipv6Addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);

// This fails!
const NETWORK: Option<Ipv6Network> = Ipv6Network::new_checked(ADDR, PREFIX);
assert_eq!(NETWORK.unwrap().prefix(), PREFIX);
Source

pub fn with_netmask( netaddr: Ipv6Addr, netmask: Ipv6Addr, ) -> Result<Ipv6Network, IpNetworkError>

Constructs a new Ipv6Network 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) -> Ipv6NetworkIterator

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

ยงWarning

This can return up to 2^128 addresses, which will take a long time to iterate over.

Source

pub fn ip(&self) -> Ipv6Addr

Source

pub fn prefix(&self) -> u8

Source

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

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

Source

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

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

Source

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

Checks if the given Ipv6Network is partly contained in other.

Source

pub fn mask(&self) -> Ipv6Addr

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

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "ff01::0".parse().unwrap();
assert_eq!(net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff));
let net: Ipv6Network = "ff01::0/32".parse().unwrap();
assert_eq!(net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));
Source

pub fn network(&self) -> Ipv6Addr

Returns the address of the network denoted by this Ipv6Network. This means the lowest possible IPv6 address inside of the network.

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "2001:db8::/96".parse().unwrap();
assert_eq!(net.network(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
Source

pub fn broadcast(&self) -> Ipv6Addr

Returns the broadcast address of this Ipv6Network. This means the highest possible IPv4 address inside of the network.

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "2001:db8::/96".parse().unwrap();
assert_eq!(net.broadcast(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0xffff, 0xffff));
Source

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

Checks if a given Ipv6Addr is in this Ipv6Network

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "ff01::0/32".parse().unwrap();
assert!(net.contains(Ipv6Addr::new(0xff01, 0, 0, 0, 0, 0, 0, 0x1)));
assert!(!net.contains(Ipv6Addr::new(0xffff, 0, 0, 0, 0, 0, 0, 0x1)));
Source

pub fn size(&self) -> u128

Returns number of possible host addresses in this Ipv6Network.

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "ff01::0/32".parse().unwrap();
assert_eq!(net.size(), 79228162514264337593543950336);

let tinynet: Ipv6Network = "ff01::0/128".parse().unwrap();
assert_eq!(tinynet.size(), 1);
Source

pub fn nth(self, n: u128) -> Option<Ipv6Addr>

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

ยงExamples
use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let net: Ipv6Network = "ff01::0/32".parse().unwrap();
assert_eq!(net.nth(0).unwrap(), "ff01::0".parse::<Ipv6Addr>().unwrap());
assert_eq!(net.nth(255).unwrap(), "ff01::ff".parse::<Ipv6Addr>().unwrap());
assert_eq!(net.nth(65538).unwrap(), "ff01::1:2".parse::<Ipv6Addr>().unwrap());
assert!(net.nth(net.size()).is_none());

Trait Implementationsยง

Sourceยง

impl Clone for Ipv6Network

Sourceยง

fn clone(&self) -> Ipv6Network

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 Ipv6Network

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl Display for Ipv6Network

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl From<Ipv6Addr> for Ipv6Network

Sourceยง

fn from(a: Ipv6Addr) -> Ipv6Network

Converts to this type from the input type.
Sourceยง

impl From<Ipv6Network> for IpNetwork

Sourceยง

fn from(v6: Ipv6Network) -> IpNetwork

Converts to this type from the input type.
Sourceยง

impl FromStr for Ipv6Network

Creates an Ipv6Network from parsing a string in CIDR notation.

ยงExamples

use std::net::Ipv6Addr;
use ipnetwork::Ipv6Network;

let new = Ipv6Network::new(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2), 65).unwrap();
let from_cidr: Ipv6Network = "FF01:0:0:17:0:0:0:2/65".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<Ipv6Network, <Ipv6Network as FromStr>::Err>

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

impl Hash for Ipv6Network

Sourceยง

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

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 &Ipv6Network

Sourceยง

type IntoIter = Ipv6NetworkIterator

Which kind of iterator are we turning this into?
Sourceยง

type Item = Ipv6Addr

The type of the elements being iterated over.
Sourceยง

fn into_iter(self) -> Ipv6NetworkIterator

Creates an iterator from a value. Read more
Sourceยง

impl Ord for Ipv6Network

Sourceยง

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

Sourceยง

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

Sourceยง

fn partial_cmp(&self, other: &Ipv6Network) -> 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 TryFrom<&str> for Ipv6Network

Sourceยง

type Error = IpNetworkError

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

fn try_from( s: &str, ) -> Result<Ipv6Network, <Ipv6Network as TryFrom<&str>>::Error>

Performs the conversion.
Sourceยง

impl Copy for Ipv6Network

Sourceยง

impl Eq for Ipv6Network

Sourceยง

impl StructuralPartialEq for Ipv6Network

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self> โ“˜

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ“˜
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more