summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-10-05 02:49:51 +0000
committerbors <bors@rust-lang.org>2020-10-05 02:49:51 +0000
commitefbaa413061c2a6e52f06f00a60ee7830fcf3ea5 (patch)
treed728ec6e5c6bbcaaf0accc89b53a176515718745 /library/std/src
parentced813fec0fb9e883906f18b76d618baf9f5bc08 (diff)
parent9dbc9ed870a3956d938c823338ac8943377845e8 (diff)
downloadrust-efbaa413061c2a6e52f06f00a60ee7830fcf3ea5.tar.gz
rust-efbaa413061c2a6e52f06f00a60ee7830fcf3ea5.zip
Auto merge of #77557 - Dylan-DPC:rollup-aib9ptp, r=Dylan-DPC
Rollup of 11 pull requests

Successful merges:

 - #75853 (Use more intra-doc-links in `core::fmt`)
 - #75928 (Remove trait_selection error message in specific case)
 - #76329 (Add check for doc alias attribute at crate level)
 - #77219 (core::global_allocator docs link to std::alloc::GlobalAlloc)
 - #77395 (BTreeMap: admit the existence of leaf edges in comments)
 - #77407 (Improve build-manifest to work with the improved promote-release)
 - #77426 (Include scope id in SocketAddrV6::Display)
 - #77439 (Fix missing diagnostic span for `impl Trait` with const generics, and add various tests for `min_const_generics` and `const_generics`)
 - #77471 (BTreeMap: refactoring around edges, missed spots)
 - #77512 (Allow `Abort` terminators in all const-contexts)
 - #77514 (Replace some once(x).chain(once(y)) with [x, y] IntoIter)

Failed merges:

r? `@ghost`
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/net/addr.rs12
-rw-r--r--library/std/src/net/addr/tests.rs10
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]