Skip to content

Commit

Permalink
[net-types] Change IP Display to match rust #112606
Browse files Browse the repository at this point in the history
This CL changes the Display impl for net_types::ip::Ipv6Addr to match
std::net::Ipv6Addr and re-enables the `test_ipv6_display` test which was
disabled in Ie459a53094b958b752e75e5cf86baa6f55bdade6.

The change follows rust-lang/rust#112606, which
stops formatting IPv4-compatible addresses as "::a.b.c.d". IPv4-mapped
addresses continue to be treated specially ("::ffff:a.b.c.d").

Test: fx test net_types_lib_test -- ip::tests::test_ipv6_display
Bug: 126927
Change-Id: I854f2bf8ba42b3e1eedceb2a5a70e8ca1da17a4b
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/873716
Reviewed-by: Joshua Liebow-Feeser <[email protected]>
Commit-Queue: Mitchell Kember <[email protected]>
  • Loading branch information
Mitchell Kember authored and Rebase bot committed Jun 30, 2023
1 parent c960460 commit 3f2e471
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/connectivity/lib/net-types/src/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,16 +1933,13 @@ impl Display for Ipv6Addr {
// by creating a scratch buffer, calling `fmt_inner` on that scratch
// buffer, and then satisfying those requirements.
fn fmt_inner<W: fmt::Write>(addr: &Ipv6Addr, w: &mut W) -> Result<(), fmt::Error> {
// We special-case the unspecified and localhost addresses because
// both addresses are valid IPv4-compatible addresses, and so if we
// don't special case them, they'll get printed as IPv4-compatible
// addresses ("::0.0.0.0" and "::0.0.0.1") respectively.
// We special-case the unspecified address, localhost address, and
// IPv4-mapped addresses, but not IPv4-compatible addresses. We
// follow Rust's behavior here: https://github.com/rust-lang/rust/pull/112606
if !addr.is_specified() {
write!(w, "::")
} else if addr.is_loopback() {
write!(w, "::1")
} else if let Some(v4) = addr.to_ipv4_compatible() {
write!(w, "::{}", v4)
} else if let Some(v4) = addr.to_ipv4_mapped() {
write!(w, "::ffff:{}", v4)
} else {
Expand Down Expand Up @@ -3658,9 +3655,6 @@ mod tests {
}

#[test]
// TODO(fxbug.dev/129316): Temporarily disabled for toolchain roll.
// Change to match https://github.com/rust-lang/rust/pull/112606 and re-enable.
#[ignore]
fn test_ipv6_display() {
// Test that `addr` is formatted the same by our `Display` impl as by
// the standard library's `Display` impl. Optionally test that it
Expand Down

0 comments on commit 3f2e471

Please sign in to comment.