Trait PrefixRange

Source
pub trait PrefixRange:
    Clone
    + Debug
    + Display
    + From<Self::Prefix>
    + FromStr<Err = Error>
    + Hash
    + IntoIterator<Item = Self::Prefix>
    + PartialEq
    + Eq
    + PartialOrd {
    type Prefix: Prefix<Length = Self::Length>;
    type Length: Length;

    // Required methods
    fn prefix(&self) -> Self::Prefix;
    fn lower(&self) -> Self::Length;
    fn upper(&self) -> Self::Length;
    fn with_intersection(
        self,
        len_range: RangeInclusive<Self::Length>,
    ) -> Option<Self>;
    fn with_length_range(
        self,
        len_range: RangeInclusive<Self::Length>,
    ) -> Option<Self>;

    // Provided methods
    fn or_longer(self) -> Self { ... }
    fn or_longer_excl(self) -> Option<Self> { ... }
    fn with_length(self, len: Self::Length) -> Option<Self> { ... }
    fn afi(&self) -> Afi { ... }
    fn new_prefix_length(&self, length: u8) -> Result<Self::Length, Error> { ... }
}
Expand description

Address-family independent interface for IP prefix ranges.

See also concrete::PrefixRange<A> and any::PrefixRange for address-family specific items.

Required Associated Types§

Source

type Prefix: Prefix<Length = Self::Length>

The type of IP prefix over which Self represents a range.

Source

type Length: Length

The type used to represent lengths for this IP prefix type.

Required Methods§

Source

fn prefix(&self) -> Self::Prefix

Return the covering super-prefix of self.

Source

fn lower(&self) -> Self::Length

Return the lower bound Self::Length of self.

Source

fn upper(&self) -> Self::Length

Return the upper bound Self::Length of self.

Source

fn with_intersection( self, len_range: RangeInclusive<Self::Length>, ) -> Option<Self>

Construct a new IP prefix-range from the intersection of self and len_range.

§Examples
use ip::{traits::PrefixRange as _, Ipv6, Prefix, PrefixLength, PrefixRange};

let lower = PrefixLength::<Ipv6>::from_primitive(52)?;
let upper = PrefixLength::<Ipv6>::from_primitive(56)?;

let x: PrefixRange<Ipv6> = "2001:db8::/48".parse::<Prefix<Ipv6>>()?.into();
assert_eq!(x.with_intersection(lower..=upper).into_iter().count(), 0,);

let y: PrefixRange<Ipv6> = "2001:db8::/54".parse::<Prefix<Ipv6>>()?.into();
assert_eq!(y.with_intersection(lower..=upper).into_iter().count(), 1,);
Source

fn with_length_range( self, len_range: RangeInclusive<Self::Length>, ) -> Option<Self>

Construct a new IP prefix-range consisting of all the more specific sub-prefixes of self with prefix-lengths within len_range.

§Examples
use core::cmp::max;

use ip::{traits::PrefixRange as _, Address, Ipv4, Prefix, PrefixLength, PrefixRange};

let addr = "192.0.2.0".parse::<Address<Ipv4>>()?;

let [l, m, n, p, q]: &[PrefixLength<Ipv4>] = &[24u8, 26, 28, 30, 32]
    .into_iter()
    .map(PrefixLength::<Ipv4>::from_primitive)
    .collect::<Result<Vec<PrefixLength<Ipv4>>, _>>()?
else {
    panic!()
};

let prefix = Prefix::<Ipv4>::new(addr, *l);

assert_eq!(
    PrefixRange::<Ipv4>::new(prefix, *m..=*n)?
        .with_length_range(*p..=*q)
        .unwrap(),
    PrefixRange::<Ipv4>::new(prefix, max(*m, *p)..=*q)?,
);

Provided Methods§

Source

fn or_longer(self) -> Self

Construct a new IP prefix-range consisting of all the more-specific sub-prefixes of self.

§Examples
use ip::{traits::PrefixRange as _, Any, Prefix, PrefixRange};

let range: PrefixRange<Any> = "2001:db8::/126".parse::<Prefix<Any>>()?.into();

assert_eq!(range.or_longer().into_iter().count(), 7,);
Source

fn or_longer_excl(self) -> Option<Self>

Construct a new IP prefix-range consisting of all the strictly more-specific sub-prefixes of self.

§Examples
use ip::{traits::PrefixRange as _, Any, Prefix, PrefixRange};

let x: PrefixRange<Any> = "192.0.2.0/24,25,27".parse()?;
let y: PrefixRange<Any> = "192.0.2.0/24,26,32".parse()?;

assert_eq!(x.or_longer_excl().unwrap(), y);
Source

fn with_length(self, len: Self::Length) -> Option<Self>

Construct a new IP prefix-range consisting of all the more-specific sub-prefixes of self of length len.

§Examples
use ip::{traits::PrefixRange as _, Any, Ipv6, Prefix, PrefixLength, PrefixRange};

let range: PrefixRange<Any> = "2001:db8::/32".parse::<Prefix<Any>>()?.into();
let len = PrefixLength::<Ipv6>::from_primitive(48)?.into();

assert_eq!(range.with_length(len).unwrap().into_iter().count(), 1 << 16,);
Source

fn afi(&self) -> Afi

Returns the address-family associated with this IP prefix-range.

§Examples
use ip::{traits::PrefixRange as _, Any, PrefixRange};

let range: PrefixRange<Any> = "2001:db8::/32,48,64".parse()?;

assert_eq!(range.afi().to_string(), "ipv6");
Source

fn new_prefix_length(&self, length: u8) -> Result<Self::Length, Error>

Try to construct a new Self::Length for the address-family associated with this IP prefix-range.

§Errors

Fails when length is outside of the bounds of prefix-lengths of the address-family.

§Examples
use ip::{traits::PrefixRange as _, Any, Ipv4, PrefixLength, PrefixRange};

let range: PrefixRange<Any> = "192.0.2.0/24,26,28".parse()?;

assert_eq!(
    range.new_prefix_length(30)?,
    PrefixLength::<Ipv4>::from_primitive(30)?.into(),
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§