diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 249f1c76680c1..05e3d38b17eb1 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -565,6 +565,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 {} @@ -582,6 +602,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 { @@ -1040,6 +1080,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 {} @@ -1057,6 +1117,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 { @@ -1458,10 +1538,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]