diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-10-05 02:29:35 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-05 02:29:35 +0200 |
| commit | f1afed541e7676860ec2162eed47a5dcd2043b18 (patch) | |
| tree | fb3c308fe50a9985ebba3ce6db8fa8b873177f2e | |
| parent | fffeaa7b83df07600092ac5b2a3d84e5dc76637b (diff) | |
| parent | 4585c22818abb87cb87cbdc5163b702f6d7ed8ca (diff) | |
| download | rust-f1afed541e7676860ec2162eed47a5dcd2043b18.tar.gz rust-f1afed541e7676860ec2162eed47a5dcd2043b18.zip | |
Rollup merge of #77426 - tamird:sockaddr-scope-id, r=dtolnay
Include scope id in SocketAddrV6::Display r? @tmandry I couldn't find any unit tests for these functions. cc @ghanan94 @brunowonka
| -rw-r--r-- | library/std/src/net/addr.rs | 12 | ||||
| -rw-r--r-- | library/std/src/net/addr/tests.rs | 10 |
2 files changed, 19 insertions, 3 deletions
diff --git a/library/std/src/net/addr.rs b/library/std/src/net/addr.rs index e213963d250..63de8712834 100644 --- a/library/std/src/net/addr.rs +++ b/library/std/src/net/addr.rs @@ -623,19 +623,27 @@ impl fmt::Display for SocketAddrV6 { // Fast path: if there's no alignment stuff, write to the output // buffer directly 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 { const IPV6_SOCKET_BUF_LEN: usize = (4 * 8) // The address + 7 // The colon separators + 2 // The brackets + + 1 + 10 // The scope id + 1 + 5; // The port let mut buf = [0; IPV6_SOCKET_BUF_LEN]; 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 // buffer is infallible - write!(buf_slice, "[{}]:{}", self.ip(), self.port()).unwrap(); + .unwrap(); let len = IPV6_SOCKET_BUF_LEN - buf_slice.len(); // This unsafe is OK because we know what is being written to the buffer diff --git a/library/std/src/net/addr/tests.rs b/library/std/src/net/addr/tests.rs index cee9087e13b..43f965de25e 100644 --- a/library/std/src/net/addr/tests.rs +++ b/library/std/src/net/addr/tests.rs @@ -178,13 +178,21 @@ fn socket_v4_to_str() { #[test] 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!("{:<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::"); + + 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] |
