Rollup merge of #112606 - clarfonthey:ip-display, r=thomcc
Alter `Display` for `Ipv6Addr` for IPv4-compatible addresses ACP: rust-lang/libs-team#239
This commit is contained in:
commit
e24fe97bd9
4 changed files with 7 additions and 13 deletions
|
@ -1770,14 +1770,8 @@ impl fmt::Display for Ipv6Addr {
|
||||||
f.write_str("::")
|
f.write_str("::")
|
||||||
} else if self.is_loopback() {
|
} else if self.is_loopback() {
|
||||||
f.write_str("::1")
|
f.write_str("::1")
|
||||||
} else if let Some(ipv4) = self.to_ipv4() {
|
} else if let Some(ipv4) = self.to_ipv4_mapped() {
|
||||||
match segments[5] {
|
write!(f, "::ffff:{}", ipv4)
|
||||||
// IPv4 Compatible address
|
|
||||||
0 => write!(f, "::{}", ipv4),
|
|
||||||
// IPv4 Mapped address
|
|
||||||
0xffff => write!(f, "::ffff:{}", ipv4),
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
#[derive(Copy, Clone, Default)]
|
#[derive(Copy, Clone, Default)]
|
||||||
struct Span {
|
struct Span {
|
||||||
|
|
|
@ -139,7 +139,7 @@ fn ipv6_addr_to_string() {
|
||||||
|
|
||||||
// ipv4-compatible address
|
// ipv4-compatible address
|
||||||
let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
|
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
|
// 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");
|
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() {
|
||||||
|
|
||||||
check!("::", unspec);
|
check!("::", unspec);
|
||||||
check!("::1", loopback);
|
check!("::1", loopback);
|
||||||
check!("::0.0.0.2", global);
|
check!("::2", global);
|
||||||
check!("1::", global);
|
check!("1::", global);
|
||||||
check!("fc00::");
|
check!("fc00::");
|
||||||
check!("fdff:ffff::");
|
check!("fdff:ffff::");
|
||||||
|
@ -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!("::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);
|
check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ fn ipv6_socket_addr_to_string() {
|
||||||
// IPv4-compatible address.
|
// IPv4-compatible address.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
|
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.
|
// IPv6 address with no zero segments.
|
||||||
|
|
|
@ -85,7 +85,7 @@ fn ipv6_socket_addr_to_string() {
|
||||||
// IPv4-compatible address.
|
// IPv4-compatible address.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
|
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.
|
// IPv6 address with no zero segments.
|
||||||
|
|
Loading…
Add table
Reference in a new issue