pub struct PrefixSet<A: Afi> { /* private fields */ }
std
only.Expand description
A collection of IP prefixes, providing fast insertion and iteration, and set-theorectic arithmetic.
This is a Rust implementation derived in large part from the internal
data-structure used in the widely used bgpq3
tool by Alexandre Snarskii,
packaged as a library, and with set-theoretic operations added.
§Examples
use ip::{traits::PrefixSet as _, Error, Ipv6, Prefix, PrefixLength, PrefixRange, PrefixSet};
fn main() -> Result<(), Error> {
// create a set by parsing a Vec<&str>
let set: PrefixSet<Ipv6> = ["2001:db8::/37", "2001:db8:f00::/37"]
.into_iter()
.map(|s| s.parse::<Prefix<Ipv6>>())
.collect::<Result<_, _>>()?;
// create a range by parsing a &str and providing the lower
// and upper prefix lenth bounds
let length = PrefixLength::<Ipv6>::from_primitive(37)?;
let range = PrefixRange::<Ipv6>::new("2001:db8::/36".parse()?, length..=length)?;
assert_eq!(set.ranges().collect::<Vec<_>>(), vec![range]);
Ok(())
}
Most mutating methods return &mut Self
for easy chaining, e.g.:
let set = PrefixSet::<Ipv4>::new()
.insert("192.0.2.0/24".parse::<Prefix<Ipv4>>()?)
.to_owned();
assert_eq!(set.len(), 1);
Implementations§
Source§impl<A: Afi> Set<A>
impl<A: Afi> Set<A>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Construct a new, empty PrefixSet<A>
.
Sourcepub fn insert<T>(&mut self, item: T) -> &mut Selfwhere
T: Into<Node<A>>,
pub fn insert<T>(&mut self, item: T) -> &mut Selfwhere
T: Into<Node<A>>,
Insert a new item
into self
.
T
can be either a Prefix<A>
or a
PrefixRange<A>
.
let range: PrefixRange<Ipv6> = "2001:db8:f00::/48,64,64".parse()?;
let set = PrefixSet::<Ipv6>::new().insert(range).to_owned();
assert_eq!(set.len(), 1 << 16);
Sourcepub fn insert_from<I, T>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: Into<Node<A>>,
pub fn insert_from<I, T>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: Into<Node<A>>,
Insert items into self
from an iterator yielding either
Prefix<A>
or
PrefixRange<A>
.
Aggregation occurs after all items are inserted, making this far more
efficient than calling PrefixSet::insert()
repeatedly.
let prefixes: Vec<_> = ["192.0.2.0/26", "192.0.2.64/26"]
.into_iter()
.map(|s| s.parse::<Prefix<Ipv4>>())
.collect::<Result<_, _>>()?;
let set = PrefixSet::<Ipv4>::new().insert_from(prefixes).to_owned();
assert_eq!(set.len(), 2);
Sourcepub fn remove<T>(&mut self, item: T) -> &mut Selfwhere
T: Into<Node<A>>,
pub fn remove<T>(&mut self, item: T) -> &mut Selfwhere
T: Into<Node<A>>,
Remove an item
from self
.
T
can be either a Prefix<A>
or a
PrefixRange<A>
.
let set = ["2001:db8:f00::/48", "2001:db8:baa::/48"]
.into_iter()
.map(|s| s.parse::<Prefix<Ipv6>>())
.collect::<Result<PrefixSet<Ipv6>, _>>()?
.remove("2001:db8:f00::/48".parse::<Prefix<Ipv6>>()?)
.to_owned();
assert_eq!(set.len(), 1);
Sourcepub fn remove_from<I, T>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: Into<Node<A>>,
pub fn remove_from<I, T>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = T>,
T: Into<Node<A>>,
Remove items from self
from an iterator yielding either
Prefix<A>
or
PrefixRange<A>
.
Aggregation occurs after all items are removed, making this far more
efficient than calling PrefixSet::remove()
repeatedly.
let prefixes: Vec<_> = vec!["192.0.2.0/26", "192.0.2.64/26"]
.into_iter()
.map(|s| s.parse::<Prefix<Ipv4>>())
.collect::<Result<_, _>>()?;
let mut set = PrefixSet::<Ipv4>::new()
.insert("192.0.2.0/24,26,26".parse::<PrefixRange<Ipv4>>()?)
.to_owned();
assert_eq!(set.remove_from(prefixes).len(), 2);
Trait Implementations§
Source§impl<A: Afi, U> Extend<U> for Set<A>where
U: Into<Node<A>>,
impl<A: Afi, U> Extend<U> for Set<A>where
U: Into<Node<A>>,
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = U>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = U>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<A: Afi, T> FromIterator<T> for Set<A>where
T: Into<Node<A>>,
impl<A: Afi, T> FromIterator<T> for Set<A>where
T: Into<Node<A>>,
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
Source§impl<A: Afi> PartialOrd for Set<A>
impl<A: Afi> PartialOrd for Set<A>
Source§impl<'a, A: Afi> Set<'a> for Set<A>
impl<'a, A: Afi> Set<'a> for Set<A>
impl<A: Afi> Eq for Set<A>
Auto Trait Implementations§
impl<A> Freeze for Set<A>
impl<A> RefUnwindSafe for Set<A>where
<<A as Afi>::Primitive as Address<A>>::LengthMap: RefUnwindSafe,
<A as Afi>::Primitive: RefUnwindSafe,
<<A as Afi>::Primitive as Address<A>>::Length: RefUnwindSafe,
impl<A> Send for Set<A>
impl<A> Sync for Set<A>
impl<A> Unpin for Set<A>
impl<A> UnwindSafe for Set<A>where
<<A as Afi>::Primitive as Address<A>>::LengthMap: UnwindSafe,
<A as Afi>::Primitive: UnwindSafe,
<<A as Afi>::Primitive as Address<A>>::Length: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.