Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 12 pull requests #72716

Merged
merged 26 commits into from
May 29, 2020
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
27fc7e5
Implement PartialOrd and Ord for SocketAddr*
hch12907 May 16, 2020
2df69ba
Stabilize str_strip feature
tesuji May 22, 2020
716acff
Remove heterogeneous ordering for SocketAddr
hch12907 May 24, 2020
d1bc8ad
Fix tests
hch12907 May 24, 2020
42ddcd3
Add myself to .mailmap
ldm0 May 27, 2020
81f8ee4
Store `LocalDefId` directly in `rustc_resolve::Resolver` where possible
marmeladema May 24, 2020
7b11879
expand unaligned_references test
RalfJung May 27, 2020
5548e69
Add working example for E0617 explanation
GuillaumeGomez May 26, 2020
1ab0db1
Fix incorrect comment in generator test
jonas-schievink May 27, 2020
b1063b8
Clippy should always build
Mark-Simulacrum May 27, 2020
e069524
Add test for #66930
JohnTitor May 28, 2020
bb745d6
update data layout for illumos x86
jclulow May 28, 2020
4329261
Remove rustc-ux-guidelines
ehuss May 28, 2020
1eef0c3
rustc_lint: Remove `unused_crate_dependencies` from the `unused` group
petrochenkov May 28, 2020
b18c55b
Rollup merge of #72239 - hch12907:master, r=dtolnay
Dylan-DPC May 29, 2020
3bcf697
Rollup merge of #72466 - lzutao:stabilize_str-strip, r=dtolnay
Dylan-DPC May 29, 2020
235f382
Rollup merge of #72605 - GuillaumeGomez:cleanup-e0617, r=Dylan-DPC
Dylan-DPC May 29, 2020
48ea37b
Rollup merge of #72636 - marmeladema:resolver-outputs-def-id, r=petro…
Dylan-DPC May 29, 2020
ec0cd2d
Rollup merge of #72645 - ldm0:mailmap, r=nikomatsakis
Dylan-DPC May 29, 2020
733bee5
Rollup merge of #72667 - RalfJung:unaligned-ref-test, r=nikomatsakis
Dylan-DPC May 29, 2020
0204fc3
Rollup merge of #72670 - rust-lang:jonas-schievink-patch-1, r=nikomat…
Dylan-DPC May 29, 2020
feaceb2
Rollup merge of #72674 - Mark-Simulacrum:clippy-always-test-pass, r=o…
Dylan-DPC May 29, 2020
71512e4
Rollup merge of #72682 - JohnTitor:mir-tests, r=RalfJung
Dylan-DPC May 29, 2020
c9f07d3
Rollup merge of #72695 - jclulow:illumos-llvm-catchup, r=nikic
Dylan-DPC May 29, 2020
ada9c11
Rollup merge of #72697 - ehuss:rm-rustc-ux-guidelines, r=nikomatsakis
Dylan-DPC May 29, 2020
049b6dd
Rollup merge of #72702 - petrochenkov:unudeps, r=estebank
Dylan-DPC May 29, 2020
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
107 changes: 106 additions & 1 deletion src/libstd/net/addr.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}