ipcheck_rs

Struct IpRange

Source
pub struct IpRange<N: IpNet> { /* private fields */ }
Expand description

A set of networks that supports various operations:

IntoIter is implemented for &IpRange. So, you can use for to iterate over the networks in an IpRange:


use iprange::IpRange;
use ipnet::Ipv4Net;

fn main() {
    let ip_range: IpRange<Ipv4Net> = ["172.16.0.0/16", "192.168.1.0/24"]
        .iter()
        .map(|s| s.parse().unwrap())
        .collect();

    for network in &ip_range {
        println!("{:?}", network);
    }
}

Implementations§

Source§

impl<N: IpNet> IpRange<N>

Source

pub fn new() -> IpRange<N>

Creates an empty IpRange.

Source

pub fn into_trie(self) -> IpTrie<N>

Source

pub fn add(&mut self, network: N) -> &mut Self

Add a network to self.

Returns &mut self in order to enable method chaining.

Pay attention that this operation will not combine two networks automatically. To do this, call simplify method explicitly. For example:

extern crate iprange;
extern crate ipnet;

use iprange::IpRange;
use ipnet::Ipv4Net;

fn main() {
    let mut ip_range: IpRange<Ipv4Net> = IpRange::new();
    ip_range.add("192.168.0.0/24".parse().unwrap())
           .add("192.168.1.0/24".parse().unwrap());
    assert_eq!(ip_range.into_iter().count(), 2);

    ip_range.simplify();
    assert_eq!(ip_range.into_iter().count(), 1);
}
Source

pub fn remove(&mut self, network: N) -> &mut Self

Remove a network from self.

Returns &mut self in order to enable method chaining.

self does not necessarily has exactly the network to be removed. The network can be a networkwork of a network in self. This method will do splitting and remove the corresponding network. For example:

extern crate iprange;
extern crate ipnet;

use iprange::IpRange;
use ipnet::Ipv4Net;

fn main() {
    let mut ip_range: IpRange<Ipv4Net> = IpRange::new();
    ip_range.add("192.168.0.0/23".parse().unwrap())
            .remove("192.168.0.0/24".parse().unwrap());
    // Now, ip_range has only one network: "192.168.1.0/24".
}
Source

pub fn is_empty(&self) -> bool

Returns true if the self has no network.

§Examples
let mut ip_range = IpRange::new();
let network: Ipv4Net = "1.0.1.0/24".parse().unwrap();
ip_range.add(network.clone());
ip_range.remove(network);
assert!(ip_range.is_empty());
Source

pub fn simplify(&mut self)

Simplify self by combining networks. For example:

extern crate iprange;
extern crate ipnet;

use iprange::IpRange;
use ipnet::Ipv4Net;

fn main() {
    let mut ip_range: IpRange<Ipv4Net> = IpRange::new();
    ip_range
        .add("192.168.0.0/20".parse().unwrap())
        .add("192.168.16.0/22".parse().unwrap())
        .add("192.168.20.0/24".parse().unwrap())
        .add("192.168.21.0/24".parse().unwrap())
        .add("192.168.22.0/24".parse().unwrap())
        .add("192.168.23.0/24".parse().unwrap())
        .add("192.168.24.0/21".parse().unwrap())
        .simplify();
    // Now, ip_range has only one network: "192.168.0.0/19".
}
Source

pub fn merge(&self, other: &IpRange<N>) -> Self

Returns a new IpRange which contains all networks that is either in self or in other.

The returned IpRange is simplified.

Source

pub fn intersect(&self, other: &IpRange<N>) -> Self

Returns a new IpRange which contains all networks that is in both self and other.

The returned IpRange is simplified.

Source

pub fn exclude(&self, other: &IpRange<N>) -> IpRange<N>

Returns a new IpRange which contains all networks that is in self while not in other.

The returned IpRange is simplified.

Source

pub fn contains<T: ToNetwork<N>>(&self, network: &T) -> bool

Tests if self contains network.

network is anything that can be converted into N. See ToNetwork<N> for detail.

Source

pub fn supernet<T: ToNetwork<N>>(&self, network: &T) -> Option<N>

Returns the network in self which is the supernetwork of network.

Returns None if no network in self contains network.

Source

pub fn iter(&self) -> IpRangeIter<'_, N>

Returns the iterator to &self.

Trait Implementations§

Source§

impl<N: Clone + IpNet> Clone for IpRange<N>

Source§

fn clone(&self) -> IpRange<N>

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<N: IpNet> Debug for IpRange<N>

Source§

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

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

impl<N> Default for IpRange<N>
where N: IpNet + ToNetwork<N> + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<N: IpNet> From<Box<IpTrieNode>> for IpRange<N>

Source§

fn from(trie: Box<IpTrieNode>) -> Self

Converts to this type from the input type.
Source§

impl<N> FromIterator<N> for IpRange<N>
where N: IpNet + ToNetwork<N> + Clone,

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = N>,

Creates a value from an iterator. Read more
Source§

impl<'a, N> IntoIterator for &'a IpRange<N>
where N: IpNet + ToNetwork<N> + Clone,

Source§

type Item = N

The type of the elements being iterated over.
Source§

type IntoIter = IpRangeIter<'a, N>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<N: PartialEq + IpNet> PartialEq for IpRange<N>

Source§

fn eq(&self, other: &IpRange<N>) -> 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<N: Eq + IpNet> Eq for IpRange<N>

Source§

impl<N: IpNet> StructuralPartialEq for IpRange<N>

Auto Trait Implementations§

§

impl<N> Freeze for IpRange<N>

§

impl<N> RefUnwindSafe for IpRange<N>
where N: RefUnwindSafe,

§

impl<N> Send for IpRange<N>
where N: Send,

§

impl<N> Sync for IpRange<N>
where N: Sync,

§

impl<N> Unpin for IpRange<N>
where N: Unpin,

§

impl<N> UnwindSafe for IpRange<N>
where N: UnwindSafe,

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, 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.