Struct iprange::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:

extern crate ipnet;
extern crate 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§

Creates an empty IpRange.

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);
}

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".
}

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".
}

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

The returned IpRange is simplified.

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

The returned IpRange is simplified.

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

The returned IpRange is simplified.

Tests if self contains network.

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

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

Returns None if no network in self contains network.

Returns the iterator to &self.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.