Include scope id in SocketAddrV6::Display
This commit is contained in:
parent
0d37dca25a
commit
4585c22818
@ -623,19 +623,27 @@ impl fmt::Display for SocketAddrV6 {
|
|||||||
// Fast path: if there's no alignment stuff, write to the output
|
// Fast path: if there's no alignment stuff, write to the output
|
||||||
// buffer directly
|
// buffer directly
|
||||||
if f.precision().is_none() && f.width().is_none() {
|
if f.precision().is_none() && f.width().is_none() {
|
||||||
write!(f, "[{}]:{}", self.ip(), self.port())
|
match self.scope_id() {
|
||||||
|
0 => write!(f, "[{}]:{}", self.ip(), self.port()),
|
||||||
|
scope_id => write!(f, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const IPV6_SOCKET_BUF_LEN: usize = (4 * 8) // The address
|
const IPV6_SOCKET_BUF_LEN: usize = (4 * 8) // The address
|
||||||
+ 7 // The colon separators
|
+ 7 // The colon separators
|
||||||
+ 2 // The brackets
|
+ 2 // The brackets
|
||||||
|
+ 1 + 10 // The scope id
|
||||||
+ 1 + 5; // The port
|
+ 1 + 5; // The port
|
||||||
|
|
||||||
let mut buf = [0; IPV6_SOCKET_BUF_LEN];
|
let mut buf = [0; IPV6_SOCKET_BUF_LEN];
|
||||||
let mut buf_slice = &mut buf[..];
|
let mut buf_slice = &mut buf[..];
|
||||||
|
|
||||||
|
match self.scope_id() {
|
||||||
|
0 => write!(buf_slice, "[{}]:{}", self.ip(), self.port()),
|
||||||
|
scope_id => write!(buf_slice, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
|
||||||
|
}
|
||||||
// Unwrap is fine because writing to a sufficiently-sized
|
// Unwrap is fine because writing to a sufficiently-sized
|
||||||
// buffer is infallible
|
// buffer is infallible
|
||||||
write!(buf_slice, "[{}]:{}", self.ip(), self.port()).unwrap();
|
.unwrap();
|
||||||
let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();
|
let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();
|
||||||
|
|
||||||
// This unsafe is OK because we know what is being written to the buffer
|
// This unsafe is OK because we know what is being written to the buffer
|
||||||
|
@ -178,13 +178,21 @@ fn socket_v4_to_str() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn socket_v6_to_str() {
|
fn socket_v6_to_str() {
|
||||||
let socket: SocketAddrV6 = "[2a02:6b8:0:1::1]:53".parse().unwrap();
|
let mut socket = SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53, 0, 0);
|
||||||
|
|
||||||
assert_eq!(format!("{}", socket), "[2a02:6b8:0:1::1]:53");
|
assert_eq!(format!("{}", socket), "[2a02:6b8:0:1::1]:53");
|
||||||
assert_eq!(format!("{:<24}", socket), "[2a02:6b8:0:1::1]:53 ");
|
assert_eq!(format!("{:<24}", socket), "[2a02:6b8:0:1::1]:53 ");
|
||||||
assert_eq!(format!("{:>24}", socket), " [2a02:6b8:0:1::1]:53");
|
assert_eq!(format!("{:>24}", socket), " [2a02:6b8:0:1::1]:53");
|
||||||
assert_eq!(format!("{:^24}", socket), " [2a02:6b8:0:1::1]:53 ");
|
assert_eq!(format!("{:^24}", socket), " [2a02:6b8:0:1::1]:53 ");
|
||||||
assert_eq!(format!("{:.15}", socket), "[2a02:6b8:0:1::");
|
assert_eq!(format!("{:.15}", socket), "[2a02:6b8:0:1::");
|
||||||
|
|
||||||
|
socket.set_scope_id(5);
|
||||||
|
|
||||||
|
assert_eq!(format!("{}", socket), "[2a02:6b8:0:1::1%5]:53");
|
||||||
|
assert_eq!(format!("{:<24}", socket), "[2a02:6b8:0:1::1%5]:53 ");
|
||||||
|
assert_eq!(format!("{:>24}", socket), " [2a02:6b8:0:1::1%5]:53");
|
||||||
|
assert_eq!(format!("{:^24}", socket), " [2a02:6b8:0:1::1%5]:53 ");
|
||||||
|
assert_eq!(format!("{:.18}", socket), "[2a02:6b8:0:1::1%5");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user