about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-05-29 00:42:59 +0000
committerbors <bors@rust-lang.org>2020-05-29 00:42:59 +0000
commit255c0338dc0b02f833fb1a816d76febd50c399c4 (patch)
treede0df9b38d9052cbb8a7bd60c2a421dcd933e76c /src/libstd
parent45127211566c53bac386b66909a830649182ab7a (diff)
parent049b6dd4e1dfd3f3c42d45e44a6dc2f8ae1a7a14 (diff)
Auto merge of #72716 - Dylan-DPC:rollup-wdj97ab, r=Dylan-DPC
Rollup of 12 pull requests

Successful merges:

 - #72239 (Implement PartialOrd and Ord for SocketAddr*)
 - #72466 (Stabilize str_strip feature)
 - #72605 (Add working example for E0617 explanation)
 - #72636 (Cleanup `Resolver::<clone|into>_outputs` methods)
 - #72645 (Add myself to .mailmap)
 - #72667 (expand unaligned_references test)
 - #72670 (Fix incorrect comment in generator test)
 - #72674 (Clippy should always build)
 - #72682 (Add test for #66930)
 - #72695 (update data layout for illumos x86)
 - #72697 (Remove rustc-ux-guidelines)
 - #72702 (rustc_lint: Remove `unused_crate_dependencies` from the `unused` group)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/net/addr.rs107
1 files changed, 106 insertions, 1 deletions
diff --git a/src/libstd/net/addr.rs b/src/libstd/net/addr.rs
index de6360cf020..08536de4d55 100644
--- a/src/libstd/net/addr.rs
+++ b/src/libstd/net/addr.rs
@@ -1,3 +1,4 @@
+use crate::cmp::Ordering;
 use crate::convert::TryInto;
 use crate::fmt;
 use crate::hash;
@@ -36,7 +37,7 @@ use crate::vec;
 /// assert_eq!(socket.port(), 8080);
 /// assert_eq!(socket.is_ipv4(), true);
 /// ```
-#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub enum SocketAddr {
     /// An IPv4 socket address.
@@ -653,11 +654,75 @@ impl PartialEq for SocketAddrV6 {
             && self.inner.sin6_scope_id == other.inner.sin6_scope_id
     }
 }
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialEq<SocketAddrV4> for SocketAddr {
+    fn eq(&self, other: &SocketAddrV4) -> bool {
+        match self {
+            SocketAddr::V4(v4) => v4 == other,
+            SocketAddr::V6(_) => false,
+        }
+    }
+}
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialEq<SocketAddrV6> for SocketAddr {
+    fn eq(&self, other: &SocketAddrV6) -> bool {
+        match self {
+            SocketAddr::V4(_) => false,
+            SocketAddr::V6(v6) => v6 == other,
+        }
+    }
+}
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialEq<SocketAddr> for SocketAddrV4 {
+    fn eq(&self, other: &SocketAddr) -> bool {
+        match other {
+            SocketAddr::V4(v4) => self == v4,
+            SocketAddr::V6(_) => false,
+        }
+    }
+}
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialEq<SocketAddr> for SocketAddrV6 {
+    fn eq(&self, other: &SocketAddr) -> bool {
+        match other {
+            SocketAddr::V4(_) => false,
+            SocketAddr::V6(v6) => self == v6,
+        }
+    }
+}
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Eq for SocketAddrV4 {}
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Eq for SocketAddrV6 {}
 
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialOrd for SocketAddrV4 {
+    fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl PartialOrd for SocketAddrV6 {
+    fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl Ord for SocketAddrV4 {
+    fn cmp(&self, other: &SocketAddrV4) -> Ordering {
+        self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
+    }
+}
+
+#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
+impl Ord for SocketAddrV6 {
+    fn cmp(&self, other: &SocketAddrV6) -> Ordering {
+        self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl hash::Hash for SocketAddrV4 {
     fn hash<H: hash::Hasher>(&self, s: &mut H) {
@@ -1102,4 +1167,44 @@ mod tests {
         assert!(!v6.is_ipv4());
         assert!(v6.is_ipv6());
     }
+
+    #[test]
+    fn compare() {
+        let v4_1 = "224.120.45.1:23456".parse::<SocketAddrV4>().unwrap();
+        let v4_2 = "224.210.103.5:12345".parse::<SocketAddrV4>().unwrap();
+        let v4_3 = "224.210.103.5:23456".parse::<SocketAddrV4>().unwrap();
+        let v6_1 = "[2001:db8:f00::1002]:23456".parse::<SocketAddrV6>().unwrap();
+        let v6_2 = "[2001:db8:f00::2001]:12345".parse::<SocketAddrV6>().unwrap();
+        let v6_3 = "[2001:db8:f00::2001]:23456".parse::<SocketAddrV6>().unwrap();
+
+        // equality
+        assert_eq!(v4_1, v4_1);
+        assert_eq!(v6_1, v6_1);
+        assert_eq!(v4_1, SocketAddr::V4(v4_1));
+        assert_eq!(v6_1, SocketAddr::V6(v6_1));
+        assert_eq!(SocketAddr::V4(v4_1), SocketAddr::V4(v4_1));
+        assert_eq!(SocketAddr::V6(v6_1), SocketAddr::V6(v6_1));
+        assert!(v4_1 != SocketAddr::V6(v6_1));
+        assert!(v6_1 != SocketAddr::V4(v4_1));
+        assert!(v4_1 != v4_2);
+        assert!(v6_1 != v6_2);
+
+        // compare different addresses
+        assert!(v4_1 < v4_2);
+        assert!(v6_1 < v6_2);
+        assert!(v4_2 > v4_1);
+        assert!(v6_2 > v6_1);
+
+        // compare the same address with different ports
+        assert!(v4_2 < v4_3);
+        assert!(v6_2 < v6_3);
+        assert!(v4_3 > v4_2);
+        assert!(v6_3 > v6_2);
+
+        // compare different addresses with the same port
+        assert!(v4_1 < v4_3);
+        assert!(v6_1 < v6_3);
+        assert!(v4_3 > v4_1);
+        assert!(v6_3 > v6_1);
+    }
 }