Trait ipnet::Contains
[−]
[src]
pub trait Contains<T> { fn contains(&self, other: T) -> bool; }
Provides a contains()
method to test if a network contains another network or
address.
Required Methods
fn contains(&self, other: T) -> bool
Returns true
if this network contains the given network or
address.
Examples
use std::net::IpAddr; use std::str::FromStr; use ipnet::{IpNet, Contains};
Ipv4Net
can contain Ipv4Net
and Ipv4Addr
.
let n1 = IpNet::from_str("10.1.1.0/24").unwrap(); let n2 = IpNet::from_str("10.1.1.0/26").unwrap(); let n3 = IpNet::from_str("10.1.2.0/26").unwrap(); let ip1 = IpAddr::from_str("10.1.1.1").unwrap(); let ip2 = IpAddr::from_str("10.1.2.1").unwrap(); assert!(n1.contains(&n2)); assert!(n1.contains(&ip1)); assert!(!n1.contains(&n3)); assert!(!n1.contains(&ip2));
Ipv6Net
can contain Ipv6Net
and Ipv6Addr
.
let n6_1 = IpNet::from_str("fd00::/16").unwrap(); let n6_2 = IpNet::from_str("fd00::/17").unwrap(); let n6_3 = IpNet::from_str("fd01::/17").unwrap(); let ip6_1 = IpAddr::from_str("fd00::1").unwrap(); let ip6_2 = IpAddr::from_str("fd01::1").unwrap(); assert!(n6_1.contains(&n6_2)); assert!(n6_1.contains(&ip6_1)); assert!(!n6_1.contains(&n6_3)); assert!(!n6_1.contains(&ip6_2));
Ipv4Net
and Ipv6Net
types cannot contain each other.
assert!(!n1.contains(&n6_1) || !n6_1.contains(&n1)); assert!(!n1.contains(&ip6_1) || !n6_1.contains(&ip1));