Skip to content

Commit 963131e

Browse files
committed
Derive Ord, PartialOrd and Hash for SocketAddr*
...instead of hand rolling impls, since 1. It's nicer 2. It fixes a buggy `Ord` impl of `SocketAddrV6`, which ignored half of the fields
1 parent a6dfd89 commit 963131e

File tree

1 file changed

+2
-49
lines changed

1 file changed

+2
-49
lines changed

library/core/src/net/socket_addr.rs

+2-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::cmp::Ordering;
21
use crate::fmt::{self, Write};
3-
use crate::hash;
42
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
53

64
use super::display_buffer::DisplayBuffer;
@@ -63,7 +61,7 @@ pub enum SocketAddr {
6361
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
6462
/// assert_eq!(socket.port(), 8080);
6563
/// ```
66-
#[derive(Copy, Clone, Eq, PartialEq)]
64+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
6765
#[stable(feature = "rust1", since = "1.0.0")]
6866
pub struct SocketAddrV4 {
6967
ip: Ipv4Addr,
@@ -96,7 +94,7 @@ pub struct SocketAddrV4 {
9694
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
9795
/// assert_eq!(socket.port(), 8080);
9896
/// ```
99-
#[derive(Copy, Clone, Eq, PartialEq)]
97+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
10098
#[stable(feature = "rust1", since = "1.0.0")]
10199
pub struct SocketAddrV6 {
102100
ip: Ipv6Addr,
@@ -644,48 +642,3 @@ impl fmt::Debug for SocketAddrV6 {
644642
fmt::Display::fmt(self, fmt)
645643
}
646644
}
647-
648-
#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
649-
impl PartialOrd for SocketAddrV4 {
650-
#[inline]
651-
fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
652-
Some(self.cmp(other))
653-
}
654-
}
655-
656-
#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
657-
impl PartialOrd for SocketAddrV6 {
658-
#[inline]
659-
fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
660-
Some(self.cmp(other))
661-
}
662-
}
663-
664-
#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
665-
impl Ord for SocketAddrV4 {
666-
#[inline]
667-
fn cmp(&self, other: &SocketAddrV4) -> Ordering {
668-
self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
669-
}
670-
}
671-
672-
#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
673-
impl Ord for SocketAddrV6 {
674-
#[inline]
675-
fn cmp(&self, other: &SocketAddrV6) -> Ordering {
676-
self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
677-
}
678-
}
679-
680-
#[stable(feature = "rust1", since = "1.0.0")]
681-
impl hash::Hash for SocketAddrV4 {
682-
fn hash<H: hash::Hasher>(&self, s: &mut H) {
683-
(self.port, self.ip).hash(s)
684-
}
685-
}
686-
#[stable(feature = "rust1", since = "1.0.0")]
687-
impl hash::Hash for SocketAddrV6 {
688-
fn hash<H: hash::Hasher>(&self, s: &mut H) {
689-
(self.port, &self.ip, self.flowinfo, self.scope_id).hash(s)
690-
}
691-
}

0 commit comments

Comments
 (0)