|
| 1 | +use crate::cmp::Ordering; |
1 | 2 | use crate::convert::TryInto;
|
2 | 3 | use crate::fmt;
|
3 | 4 | use crate::hash;
|
@@ -36,7 +37,7 @@ use crate::vec;
|
36 | 37 | /// assert_eq!(socket.port(), 8080);
|
37 | 38 | /// assert_eq!(socket.is_ipv4(), true);
|
38 | 39 | /// ```
|
39 |
| -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
| 40 | +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] |
40 | 41 | #[stable(feature = "rust1", since = "1.0.0")]
|
41 | 42 | pub enum SocketAddr {
|
42 | 43 | /// An IPv4 socket address.
|
@@ -653,11 +654,75 @@ impl PartialEq for SocketAddrV6 {
|
653 | 654 | && self.inner.sin6_scope_id == other.inner.sin6_scope_id
|
654 | 655 | }
|
655 | 656 | }
|
| 657 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 658 | +impl PartialEq<SocketAddrV4> for SocketAddr { |
| 659 | + fn eq(&self, other: &SocketAddrV4) -> bool { |
| 660 | + match self { |
| 661 | + SocketAddr::V4(v4) => v4 == other, |
| 662 | + SocketAddr::V6(_) => false, |
| 663 | + } |
| 664 | + } |
| 665 | +} |
| 666 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 667 | +impl PartialEq<SocketAddrV6> for SocketAddr { |
| 668 | + fn eq(&self, other: &SocketAddrV6) -> bool { |
| 669 | + match self { |
| 670 | + SocketAddr::V4(_) => false, |
| 671 | + SocketAddr::V6(v6) => v6 == other, |
| 672 | + } |
| 673 | + } |
| 674 | +} |
| 675 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 676 | +impl PartialEq<SocketAddr> for SocketAddrV4 { |
| 677 | + fn eq(&self, other: &SocketAddr) -> bool { |
| 678 | + match other { |
| 679 | + SocketAddr::V4(v4) => self == v4, |
| 680 | + SocketAddr::V6(_) => false, |
| 681 | + } |
| 682 | + } |
| 683 | +} |
| 684 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 685 | +impl PartialEq<SocketAddr> for SocketAddrV6 { |
| 686 | + fn eq(&self, other: &SocketAddr) -> bool { |
| 687 | + match other { |
| 688 | + SocketAddr::V4(_) => false, |
| 689 | + SocketAddr::V6(v6) => self == v6, |
| 690 | + } |
| 691 | + } |
| 692 | +} |
656 | 693 | #[stable(feature = "rust1", since = "1.0.0")]
|
657 | 694 | impl Eq for SocketAddrV4 {}
|
658 | 695 | #[stable(feature = "rust1", since = "1.0.0")]
|
659 | 696 | impl Eq for SocketAddrV6 {}
|
660 | 697 |
|
| 698 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 699 | +impl PartialOrd for SocketAddrV4 { |
| 700 | + fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> { |
| 701 | + Some(self.cmp(other)) |
| 702 | + } |
| 703 | +} |
| 704 | + |
| 705 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 706 | +impl PartialOrd for SocketAddrV6 { |
| 707 | + fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> { |
| 708 | + Some(self.cmp(other)) |
| 709 | + } |
| 710 | +} |
| 711 | + |
| 712 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 713 | +impl Ord for SocketAddrV4 { |
| 714 | + fn cmp(&self, other: &SocketAddrV4) -> Ordering { |
| 715 | + self.ip().cmp(other.ip()).then(self.port().cmp(&other.port())) |
| 716 | + } |
| 717 | +} |
| 718 | + |
| 719 | +#[stable(feature = "socketaddr_ordering", since = "1.45.0")] |
| 720 | +impl Ord for SocketAddrV6 { |
| 721 | + fn cmp(&self, other: &SocketAddrV6) -> Ordering { |
| 722 | + self.ip().cmp(other.ip()).then(self.port().cmp(&other.port())) |
| 723 | + } |
| 724 | +} |
| 725 | + |
661 | 726 | #[stable(feature = "rust1", since = "1.0.0")]
|
662 | 727 | impl hash::Hash for SocketAddrV4 {
|
663 | 728 | fn hash<H: hash::Hasher>(&self, s: &mut H) {
|
@@ -1102,4 +1167,44 @@ mod tests {
|
1102 | 1167 | assert!(!v6.is_ipv4());
|
1103 | 1168 | assert!(v6.is_ipv6());
|
1104 | 1169 | }
|
| 1170 | + |
| 1171 | + #[test] |
| 1172 | + fn compare() { |
| 1173 | + let v4_1 = "224.120.45.1:23456".parse::<SocketAddrV4>().unwrap(); |
| 1174 | + let v4_2 = "224.210.103.5:12345".parse::<SocketAddrV4>().unwrap(); |
| 1175 | + let v4_3 = "224.210.103.5:23456".parse::<SocketAddrV4>().unwrap(); |
| 1176 | + let v6_1 = "[2001:db8:f00::1002]:23456".parse::<SocketAddrV6>().unwrap(); |
| 1177 | + let v6_2 = "[2001:db8:f00::2001]:12345".parse::<SocketAddrV6>().unwrap(); |
| 1178 | + let v6_3 = "[2001:db8:f00::2001]:23456".parse::<SocketAddrV6>().unwrap(); |
| 1179 | + |
| 1180 | + // equality |
| 1181 | + assert_eq!(v4_1, v4_1); |
| 1182 | + assert_eq!(v6_1, v6_1); |
| 1183 | + assert_eq!(v4_1, SocketAddr::V4(v4_1)); |
| 1184 | + assert_eq!(v6_1, SocketAddr::V6(v6_1)); |
| 1185 | + assert_eq!(SocketAddr::V4(v4_1), SocketAddr::V4(v4_1)); |
| 1186 | + assert_eq!(SocketAddr::V6(v6_1), SocketAddr::V6(v6_1)); |
| 1187 | + assert!(v4_1 != SocketAddr::V6(v6_1)); |
| 1188 | + assert!(v6_1 != SocketAddr::V4(v4_1)); |
| 1189 | + assert!(v4_1 != v4_2); |
| 1190 | + assert!(v6_1 != v6_2); |
| 1191 | + |
| 1192 | + // compare different addresses |
| 1193 | + assert!(v4_1 < v4_2); |
| 1194 | + assert!(v6_1 < v6_2); |
| 1195 | + assert!(v4_2 > v4_1); |
| 1196 | + assert!(v6_2 > v6_1); |
| 1197 | + |
| 1198 | + // compare the same address with different ports |
| 1199 | + assert!(v4_2 < v4_3); |
| 1200 | + assert!(v6_2 < v6_3); |
| 1201 | + assert!(v4_3 > v4_2); |
| 1202 | + assert!(v6_3 > v6_2); |
| 1203 | + |
| 1204 | + // compare different addresses with the same port |
| 1205 | + assert!(v4_1 < v4_3); |
| 1206 | + assert!(v6_1 < v6_3); |
| 1207 | + assert!(v4_3 > v4_1); |
| 1208 | + assert!(v6_3 > v6_1); |
| 1209 | + } |
1105 | 1210 | }
|
0 commit comments