From 9301e2e65d6fda364a4d9be5eb1b37fad6a43536 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Sun, 18 Dec 2016 23:07:27 -0500 Subject: [PATCH] Comparison between IpAddr and Ipv[46]Addr. --- src/libstd/net/ip.rs | 104 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 6aab7486004f8..e409ad95cf862 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -551,6 +551,26 @@ impl PartialEq for Ipv4Addr { } } +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialEq for IpAddr { + fn eq(&self, other: &Ipv4Addr) -> bool { + match *self { + IpAddr::V4(ref v4) => v4 == other, + IpAddr::V6(_) => false, + } + } +} + +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialEq for Ipv4Addr { + fn eq(&self, other: &IpAddr) -> bool { + match *other { + IpAddr::V4(ref v4) => self == v4, + IpAddr::V6(_) => false, + } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Eq for Ipv4Addr {} @@ -568,6 +588,26 @@ impl PartialOrd for Ipv4Addr { } } +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialOrd for IpAddr { + fn partial_cmp(&self, other: &Ipv4Addr) -> Option { + match *self { + IpAddr::V4(ref v4) => v4.partial_cmp(other), + IpAddr::V6(_) => Some(Ordering::Greater), + } + } +} + +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialOrd for Ipv4Addr { + fn partial_cmp(&self, other: &IpAddr) -> Option { + match *other { + IpAddr::V4(ref v4) => self.partial_cmp(v4), + IpAddr::V6(_) => Some(Ordering::Less), + } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Ord for Ipv4Addr { fn cmp(&self, other: &Ipv4Addr) -> Ordering { @@ -1026,6 +1066,26 @@ impl PartialEq for Ipv6Addr { } } +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialEq for Ipv6Addr { + fn eq(&self, other: &IpAddr) -> bool { + match *other { + IpAddr::V4(_) => false, + IpAddr::V6(ref v6) => self == v6, + } + } +} + +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialEq for IpAddr { + fn eq(&self, other: &Ipv6Addr) -> bool { + match *self { + IpAddr::V4(_) => false, + IpAddr::V6(ref v6) => v6 == other, + } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Eq for Ipv6Addr {} @@ -1043,6 +1103,26 @@ impl PartialOrd for Ipv6Addr { } } +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialOrd for IpAddr { + fn partial_cmp(&self, other: &Ipv6Addr) -> Option { + match *self { + IpAddr::V4(_) => Some(Ordering::Less), + IpAddr::V6(ref v6) => v6.partial_cmp(other), + } + } +} + +#[stable(feature = "ip_cmp", since = "1.15.0")] +impl PartialOrd for Ipv6Addr { + fn partial_cmp(&self, other: &IpAddr) -> Option { + match *other { + IpAddr::V4(_) => Some(Ordering::Greater), + IpAddr::V6(ref v6) => self.partial_cmp(v6), + } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Ord for Ipv6Addr { fn cmp(&self, other: &Ipv6Addr) -> Ordering { @@ -1444,10 +1524,26 @@ mod tests { } #[test] - fn ord() { - assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2)); - assert!("2001:db8:f00::1002".parse::().unwrap() < - "2001:db8:f00::2001".parse::().unwrap()); + fn cmp() { + let v41 = Ipv4Addr::new(100, 64, 3, 3); + let v42 = Ipv4Addr::new(192, 0, 2, 2); + let v61 = "2001:db8:f00::1002".parse::().unwrap(); + let v62 = "2001:db8:f00::2001".parse::().unwrap(); + assert!(v41 < v42); + assert!(v61 < v62); + + assert_eq!(v41, IpAddr::V4(v41)); + assert_eq!(v61, IpAddr::V6(v61)); + assert!(v41 != IpAddr::V4(v42)); + assert!(v61 != IpAddr::V6(v62)); + + assert!(v41 < IpAddr::V4(v42)); + assert!(v61 < IpAddr::V6(v62)); + assert!(IpAddr::V4(v41) < v42); + assert!(IpAddr::V6(v61) < v62); + + assert!(v41 < IpAddr::V6(v61)); + assert!(IpAddr::V4(v41) < v61); } #[test]