Skip to content

Commit 14d3539

Browse files
ericktRebase bot
authored and
Rebase bot
committed
[ffx] Only compare TargetAddr on ip and port
This patch implements custom `PartialEq`, `Eq`, `PartialOrd`, `Ord`, and `Hash` that only compare `ip` and `port`, and ignores ipv6 `flowinfo` and `scope_id` fields. The latest rust nightly has [fixed] a longstanding bug where the `PartialEq` implementation for `std::net::SocketAddr6` did not compare the `flowinfo` and `scope_id` fields. This has been fixed, but it broke ffx, which depended on this to deduplicate addresses that had the same ip and port, but different scopes. [fixed]: rust-lang/rust#116714 Change-Id: I64536942fe3ad44c509ffa10bd8e637d2a92ea2c Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/942191 Reviewed-by: Steven Grady <[email protected]> Fuchsia-Auto-Submit: Erick Tryzelaar <[email protected]> Commit-Queue: Auto-Submit <[email protected]>
1 parent f49f748 commit 14d3539

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/developer/ffx/lib/addr/src/target_addr.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,31 @@ use std::{
1212
str::FromStr,
1313
};
1414

15-
#[derive(Hash, Clone, Debug, Copy, Eq, PartialEq)]
15+
#[derive(Clone, Debug, Copy)]
1616
pub struct TargetAddr(SocketAddr);
1717

18+
// Only compare `TargetAddr` by ip and port, since we want to deduplicate targets if they are
19+
// addressable over multiple IPv6 interfaces.
20+
impl std::hash::Hash for TargetAddr {
21+
fn hash<H>(&self, state: &mut H)
22+
where
23+
H: std::hash::Hasher,
24+
{
25+
(self.0.ip(), self.0.port()).hash(state)
26+
}
27+
}
28+
29+
impl PartialEq for TargetAddr {
30+
fn eq(&self, other: &Self) -> bool {
31+
self.0.ip() == other.0.ip() && self.0.port() == other.0.port()
32+
}
33+
}
34+
35+
impl Eq for TargetAddr {}
36+
1837
impl Ord for TargetAddr {
1938
fn cmp(&self, other: &Self) -> Ordering {
20-
let this_socket = SocketAddr::from(self);
21-
let other_socket = SocketAddr::from(other);
22-
this_socket.cmp(&other_socket)
39+
self.0.ip().cmp(&other.0.ip()).then(self.0.port().cmp(&other.0.port()))
2340
}
2441
}
2542

0 commit comments

Comments
 (0)