Skip to content

Commit 996c4ba

Browse files
Rollup merge of rust-lang#112606 - clarfonthey:ip-display, r=thomcc
Alter `Display` for `Ipv6Addr` for IPv4-compatible addresses ACP: rust-lang/libs-team#239
2 parents 8b5b472 + 2dce58d commit 996c4ba

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

library/core/src/net/ip_addr.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1770,14 +1770,8 @@ impl fmt::Display for Ipv6Addr {
17701770
f.write_str("::")
17711771
} else if self.is_loopback() {
17721772
f.write_str("::1")
1773-
} else if let Some(ipv4) = self.to_ipv4() {
1774-
match segments[5] {
1775-
// IPv4 Compatible address
1776-
0 => write!(f, "::{}", ipv4),
1777-
// IPv4 Mapped address
1778-
0xffff => write!(f, "::ffff:{}", ipv4),
1779-
_ => unreachable!(),
1780-
}
1773+
} else if let Some(ipv4) = self.to_ipv4_mapped() {
1774+
write!(f, "::ffff:{}", ipv4)
17811775
} else {
17821776
#[derive(Copy, Clone, Default)]
17831777
struct Span {

library/core/tests/net/ip_addr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn ipv6_addr_to_string() {
139139

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

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

317317
check!("::", unspec);
318318
check!("::1", loopback);
319-
check!("::0.0.0.2", global);
319+
check!("::2", global);
320320
check!("1::", global);
321321
check!("fc00::");
322322
check!("fdff:ffff::");
@@ -607,7 +607,7 @@ fn ipv6_properties() {
607607

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

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

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

library/core/tests/net/socket_addr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn ipv6_socket_addr_to_string() {
3434
// IPv4-compatible address.
3535
assert_eq!(
3636
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
37-
"[::192.0.2.128]:8080"
37+
"[::c000:280]:8080"
3838
);
3939

4040
// IPv6 address with no zero segments.

library/std/src/net/socket_addr/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn ipv6_socket_addr_to_string() {
8585
// IPv4-compatible address.
8686
assert_eq!(
8787
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
88-
"[::192.0.2.128]:8080"
88+
"[::c000:280]:8080"
8989
);
9090

9191
// IPv6 address with no zero segments.

0 commit comments

Comments
 (0)