netaddr2/netv6addr/
contains.rs

1use super::Netv6Addr;
2use crate::traits::Contains;
3use crate::traits::Mask;
4
5impl Contains<std::net::IpAddr> for Netv6Addr {
6	fn contains(&self, other: &std::net::IpAddr) -> bool {
7		match other {
8			std::net::IpAddr::V6(other) => self.contains(other),
9			_ => false,
10		}
11	}
12}
13
14impl Contains<std::net::Ipv6Addr> for Netv6Addr {
15	fn contains(&self, other: &std::net::Ipv6Addr) -> bool {
16		other.mask(&self.mask()) == self.addr()
17	}
18}
19
20impl Contains<crate::NetAddr> for Netv6Addr {
21	fn contains(&self, other: &crate::NetAddr) -> bool {
22		match other {
23			crate::NetAddr::V6(other) => self.contains(other),
24			_ => false,
25		}
26	}
27}
28
29impl Contains<Netv6Addr> for Netv6Addr {
30	fn contains(&self, other: &Netv6Addr) -> bool {
31		other.addr().mask(&self.mask()) == self.addr()
32	}
33}
34
35#[cfg(test)]
36mod tests {
37	use super::*;
38	use std::net::Ipv6Addr;
39
40	#[test]
41	fn ip() {
42		let net: Netv6Addr = "2001:db8:d00b::/48".parse().unwrap();
43		assert!(net.contains(&Ipv6Addr::new(0x2001, 0x0db8, 0xd00b, 0, 0, 0, 0, 0x0001)));
44		assert!(net.contains(&Ipv6Addr::new(
45			0x2001, 0x0db8, 0xd00b, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
46		)));
47		assert!(!net.contains(&Ipv6Addr::new(0x2001, 0x0db8, 0xd00c, 0, 0, 0, 0, 1)));
48	}
49
50	#[test]
51	fn net() {
52		let net: Netv6Addr = "2001:db8:d000::/40".parse().unwrap();
53		let net_inner: Netv6Addr = "2001:db8:d00b::/48".parse().unwrap();
54		assert!(net.contains(&net_inner));
55	}
56}