Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
963131e
Derive `Ord`, `PartialOrd` and `Hash` for `SocketAddr*`
WaffleLapkin Oct 14, 2023
5c13c69
Add tests for `SocketAddrV6` ordering with scope_id and flowinfo
WaffleLapkin Oct 15, 2023
2bba98b
Avoid unnecessary renumbering
JonasAlaif Oct 16, 2023
66a554b
Add method to convert internal to stable constructs
celinval Oct 21, 2023
f613b26
Add `internal()` method counterpart to `stable()`
celinval Oct 23, 2023
421631a
Remove unsafe and `Rc`
celinval Oct 23, 2023
cb61816
compiler: Add target features for LoongArch
heiher Oct 19, 2023
6cf9423
tests: Add features-gate for LoongArch
heiher Oct 19, 2023
300d04d
tests/ui/abi/compatibility: Set min-llvm-version to 17 for LoongArch64
heiher Oct 24, 2023
ae86f59
Add test and remove double ref
celinval Oct 24, 2023
2b36547
Introduce `-C instrument-coverage=branch` to gate branch coverage
Swatinem Aug 21, 2023
68f5536
Migrate diagnostics in `rustc_hir_analysis/src/coherence/orphan.rs`
obeis Oct 3, 2023
90e3aae
Remove incomplete features from RPITIT/AFIT tests
compiler-errors Oct 24, 2023
f3d20be
suggest unwrap/expect for let binding type mismatch
chenyukang Oct 17, 2023
c07ff9c
Rollup merge of #116094 - Swatinem:coverage-branch-gate, r=wesleywiser
matthiaskrgr Oct 24, 2023
f3e18e4
Rollup merge of #116396 - obeis:hir-analysis-migrate-diagnostics-7, r…
matthiaskrgr Oct 24, 2023
845c414
Rollup merge of #116714 - WaffleLapkin:order-the-order, r=joshtriplett
matthiaskrgr Oct 24, 2023
61ff4db
Rollup merge of #116792 - JonasAlaif:renumber-fix, r=b-naber
matthiaskrgr Oct 24, 2023
7a0a2d2
Rollup merge of #116841 - chenyukang:yukang-suggest-unwrap-expect, r=…
matthiaskrgr Oct 24, 2023
84f0bef
Rollup merge of #116943 - heiher:target-features, r=wesleywiser
matthiaskrgr Oct 24, 2023
f131a0a
Rollup merge of #117010 - celinval:smir-internal, r=oli-obk
matthiaskrgr Oct 24, 2023
060bdfd
Rollup merge of #117127 - compiler-errors:incomplete, r=lqd
matthiaskrgr Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 2 additions & 49 deletions library/core/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cmp::Ordering;
use crate::fmt::{self, Write};
use crate::hash;
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use super::display_buffer::DisplayBuffer;
Expand Down Expand Up @@ -63,7 +61,7 @@ pub enum SocketAddr {
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
/// assert_eq!(socket.port(), 8080);
/// ```
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct SocketAddrV4 {
ip: Ipv4Addr,
Expand Down Expand Up @@ -96,7 +94,7 @@ pub struct SocketAddrV4 {
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
/// assert_eq!(socket.port(), 8080);
/// ```
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct SocketAddrV6 {
ip: Ipv6Addr,
Expand Down Expand Up @@ -644,48 +642,3 @@ impl fmt::Debug for SocketAddrV6 {
fmt::Display::fmt(self, fmt)
}
}

#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
impl PartialOrd for SocketAddrV4 {
#[inline]
fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
Some(self.cmp(other))
}
}

#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
impl PartialOrd for SocketAddrV6 {
#[inline]
fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
Some(self.cmp(other))
}
}

#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
impl Ord for SocketAddrV4 {
#[inline]
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 {
#[inline]
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) {
(self.port, self.ip).hash(s)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl hash::Hash for SocketAddrV6 {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
(self.port, &self.ip, self.flowinfo, self.scope_id).hash(s)
}
}
11 changes: 11 additions & 0 deletions library/core/tests/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ fn compare() {
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();
let v6_4 = "[2001:db8:f00::2001%42]:23456".parse::<SocketAddrV6>().unwrap();
let mut v6_5 = "[2001:db8:f00::2001]:23456".parse::<SocketAddrV6>().unwrap();
v6_5.set_flowinfo(17);

// equality
assert_eq!(v4_1, v4_1);
Expand All @@ -207,6 +210,8 @@ fn compare() {
assert_eq!(SocketAddr::V6(v6_1), SocketAddr::V6(v6_1));
assert!(v4_1 != v4_2);
assert!(v6_1 != v6_2);
assert!(v6_3 != v6_4);
assert!(v6_3 != v6_5);

// compare different addresses
assert!(v4_1 < v4_2);
Expand All @@ -226,6 +231,12 @@ fn compare() {
assert!(v4_3 > v4_1);
assert!(v6_3 > v6_1);

// compare the same address with different scope_id
assert!(v6_3 < v6_4);

// compare the same address with different flowinfo
assert!(v6_3 < v6_5);

// compare with an inferred right-hand side
assert_eq!(v4_1, "224.120.45.1:23456".parse().unwrap());
assert_eq!(v6_1, "[2001:db8:f00::1002]:23456".parse().unwrap());
Expand Down