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§
source§impl<N: IpNet> IpRange<N>
impl<N: IpNet> IpRange<N>
sourcepub fn add(&mut self, network: N) -> &mut Self
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);
}
sourcepub fn remove(&mut self, network: N) -> &mut Self
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".
}
sourcepub fn simplify(&mut self)
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".
}
sourcepub fn merge(&self, other: &IpRange<N>) -> Self
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.
sourcepub fn intersect(&self, other: &IpRange<N>) -> Self
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.
sourcepub fn exclude(&self, other: &IpRange<N>) -> IpRange<N>
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.
sourcepub fn contains<T: ToNetwork<N>>(&self, network: &T) -> bool
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.
sourcepub fn supernet<T: ToNetwork<N>>(&self, network: &T) -> Option<N>
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
.
sourcepub fn iter(&self) -> IpRangeIter<N> ⓘ
pub fn iter(&self) -> IpRangeIter<N> ⓘ
Returns the iterator to &self
.