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>
impl<N: IpNet> IpRange<N>
pub fn into_trie(self) -> IpTrie<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 is_empty(&self) -> bool
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());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.