Skip to content

Commit

Permalink
[ffx] Only compare TargetAddr on ip and port
Browse files Browse the repository at this point in the history
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 <slgrady@google.com>
Fuchsia-Auto-Submit: Erick Tryzelaar <etryzelaar@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
  • Loading branch information
erickt authored and Rebase bot committed Nov 8, 2023
1 parent f49f748 commit 14d3539
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/developer/ffx/lib/addr/src/target_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@ use std::{
str::FromStr,
};

#[derive(Hash, Clone, Debug, Copy, Eq, PartialEq)]
#[derive(Clone, Debug, Copy)]
pub struct TargetAddr(SocketAddr);

// Only compare `TargetAddr` by ip and port, since we want to deduplicate targets if they are
// addressable over multiple IPv6 interfaces.
impl std::hash::Hash for TargetAddr {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
(self.0.ip(), self.0.port()).hash(state)
}
}

impl PartialEq for TargetAddr {
fn eq(&self, other: &Self) -> bool {
self.0.ip() == other.0.ip() && self.0.port() == other.0.port()
}
}

impl Eq for TargetAddr {}

impl Ord for TargetAddr {
fn cmp(&self, other: &Self) -> Ordering {
let this_socket = SocketAddr::from(self);
let other_socket = SocketAddr::from(other);
this_socket.cmp(&other_socket)
self.0.ip().cmp(&other.0.ip()).then(self.0.port().cmp(&other.0.port()))
}
}

Expand Down

0 comments on commit 14d3539

Please sign in to comment.