mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-16 08:48:51 +00:00
Implement overlaps for both types
This commit is contained in:
23
src/ipv4.rs
23
src/ipv4.rs
@ -72,6 +72,17 @@ impl Ipv4Network {
|
||||
other.is_subnet_of(self)
|
||||
}
|
||||
|
||||
/// Checks if the given `Ipv4Network` is partly contained in other.
|
||||
pub fn overlaps(self, other: Ipv4Network) -> bool {
|
||||
other.contains(self.ip()) || (
|
||||
other.contains(self.broadcast()) || (
|
||||
self.contains(other.ip()) || (
|
||||
self.contains(other.broadcast())
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the mask for this `Ipv4Network`.
|
||||
/// That means the `prefix` most significant bits will be 1 and the rest 0
|
||||
///
|
||||
@ -495,4 +506,16 @@ mod test {
|
||||
assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlaps() {
|
||||
let other: Ipv4Network = "1.2.3.0/30".parse().unwrap();
|
||||
let other2: Ipv4Network = "1.2.2.0/24".parse().unwrap();
|
||||
let other3: Ipv4Network = "1.2.2.64/26".parse().unwrap();
|
||||
|
||||
let skynet: Ipv4Network = "1.2.3.0/24".parse().unwrap();
|
||||
assert_eq!(skynet.overlaps(other), true);
|
||||
assert_eq!(skynet.overlaps(other2), false);
|
||||
assert_eq!(other2.overlaps(other3), true);
|
||||
}
|
||||
}
|
||||
|
19
src/ipv6.rs
19
src/ipv6.rs
@ -121,6 +121,17 @@ impl Ipv6Network {
|
||||
other.is_subnet_of(self)
|
||||
}
|
||||
|
||||
/// Checks if the given `Ipv6Network` is partly contained in other.
|
||||
pub fn overlaps(self, other: Ipv6Network) -> bool {
|
||||
other.contains(self.ip()) || (
|
||||
other.contains(self.broadcast()) || (
|
||||
self.contains(other.ip()) || (
|
||||
self.contains(other.broadcast())
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the mask for this `Ipv6Network`.
|
||||
/// That means the `prefix` most significant bits will be 1 and the rest 0
|
||||
///
|
||||
@ -478,4 +489,12 @@ mod test {
|
||||
assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlaps() {
|
||||
let other: Ipv6Network = "2001:DB8:ACAD::1/64".parse().unwrap();
|
||||
let other2: Ipv6Network = "2001:DB8:ACAD::20:2/64".parse().unwrap();
|
||||
|
||||
assert_eq!(other2.overlaps(other), true);
|
||||
}
|
||||
}
|
||||
|
@ -202,7 +202,7 @@ impl IpNetwork {
|
||||
|
||||
// TODO(abhishek) when TryFrom is stable, implement it for IpNetwork to
|
||||
// variant conversions. Then use that to implement a generic is_subnet_of
|
||||
// and is_supernet_of
|
||||
// is_supernet_of, overlaps
|
||||
|
||||
/// Checks if a given `IpAddr` is in this `IpNetwork`
|
||||
///
|
||||
|
Reference in New Issue
Block a user