Skip to content

Commit

Permalink
Rollup merge of rust-lang#112606 - clarfonthey:ip-display, r=thomcc
Browse files Browse the repository at this point in the history
Alter `Display` for `Ipv6Addr` for IPv4-compatible addresses

ACP: rust-lang/libs-team#239
  • Loading branch information
compiler-errors authored Jun 18, 2023
2 parents 9f4d96f + 2dce58d commit 9589d43
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
10 changes: 2 additions & 8 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,14 +1770,8 @@ impl fmt::Display for Ipv6Addr {
f.write_str("::")
} else if self.is_loopback() {
f.write_str("::1")
} else if let Some(ipv4) = self.to_ipv4() {
match segments[5] {
// IPv4 Compatible address
0 => write!(f, "::{}", ipv4),
// IPv4 Mapped address
0xffff => write!(f, "::ffff:{}", ipv4),
_ => unreachable!(),
}
} else if let Some(ipv4) = self.to_ipv4_mapped() {
write!(f, "::ffff:{}", ipv4)
} else {
#[derive(Copy, Clone, Default)]
struct Span {
Expand Down
6 changes: 3 additions & 3 deletions library/core/tests/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn ipv6_addr_to_string() {

// ipv4-compatible address
let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
assert_eq!(a1.to_string(), "::192.0.2.128");
assert_eq!(a1.to_string(), "::c000:280");

// v6 address with no zero segments
assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(), "8:9:a:b:c:d:e:f");
Expand Down Expand Up @@ -316,7 +316,7 @@ fn ip_properties() {

check!("::", unspec);
check!("::1", loopback);
check!("::0.0.0.2", global);
check!("::2", global);
check!("1::", global);
check!("fc00::");
check!("fdff:ffff::");
Expand Down Expand Up @@ -607,7 +607,7 @@ fn ipv6_properties() {

check!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback);

check!("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], global | unicast_global);
check!("::2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], global | unicast_global);

check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);

Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn ipv6_socket_addr_to_string() {
// IPv4-compatible address.
assert_eq!(
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
"[::192.0.2.128]:8080"
"[::c000:280]:8080"
);

// IPv6 address with no zero segments.
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/net/socket_addr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn ipv6_socket_addr_to_string() {
// IPv4-compatible address.
assert_eq!(
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
"[::192.0.2.128]:8080"
"[::c000:280]:8080"
);

// IPv6 address with no zero segments.
Expand Down

0 comments on commit 9589d43

Please sign in to comment.