about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorNathan West <Lucretiel@gmail.com>2020-05-20 16:29:36 -0400
committerNathan West <Lucretiel@gmail.com>2020-05-29 00:50:35 -0400
commitdefbd845a33cf3c61c3af77aae474964f92e34bb (patch)
tree1ddf044b21281d34ae83c1e5f04b30e39cd57f8c /src/libstd
parent813ce7a688f716e53f3dd22a89ec059af3b67c13 (diff)
downloadrust-defbd845a33cf3c61c3af77aae474964f92e34bb.tar.gz
rust-defbd845a33cf3c61c3af77aae474964f92e34bb.zip
Added fast-path, tests
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/net/addr.rs74
1 files changed, 54 insertions, 20 deletions
diff --git a/src/libstd/net/addr.rs b/src/libstd/net/addr.rs
index 8870c405a26..2febe157a50 100644
--- a/src/libstd/net/addr.rs
+++ b/src/libstd/net/addr.rs
@@ -600,17 +600,23 @@ impl fmt::Display for SocketAddr {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl fmt::Display for SocketAddrV4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        const IPV4_SOCKET_BUF_LEN: usize = 21;
-        let mut buf = [0; IPV4_SOCKET_BUF_LEN];
-        let mut buf_slice = &mut buf[..];
-
-        // Unwrap is fine because writing to a buffer is infallible
-        write!(buf_slice, "{}:{}", self.ip(), self.port()).unwrap();
-        let len = IPV4_SOCKET_BUF_LEN - buf_slice.len();
-
-        // This unsafe is OK because we know what is being written to the buffer
-        let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
-        f.pad(buf)
+        // 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())
+        } else {
+            const IPV4_SOCKET_BUF_LEN: usize = 21;
+            let mut buf = [0; IPV4_SOCKET_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
+
+            // Unwrap is fine because writing to a buffer is infallible
+            write!(buf_slice, "{}:{}", self.ip(), self.port()).unwrap();
+            let len = IPV4_SOCKET_BUF_LEN - buf_slice.len();
+
+            // This unsafe is OK because we know what is being written to the buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            f.pad(buf)
+        }
     }
 }
 
@@ -624,21 +630,27 @@ impl fmt::Debug for SocketAddrV4 {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl fmt::Display for SocketAddrV6 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        const IPV6_SOCKET_BUF_LEN: usize = (4 * 8)  // The address
+        // 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())
+        } else {
+            const IPV6_SOCKET_BUF_LEN: usize = (4 * 8)  // The address
             + 7  // The colon separators
             + 2  // The brackets
             + 1 + 5; // The port
 
-        let mut buf = [0; IPV6_SOCKET_BUF_LEN];
-        let mut buf_slice = &mut buf[..];
+            let mut buf = [0; IPV6_SOCKET_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
 
-        // Unwrap is fine because writing to a buffer is infallible
-        write!(buf_slice, "[{}]:{}", self.ip(), self.port()).unwrap();
-        let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();
+            // Unwrap is fine because writing to a buffer is infallible
+            write!(buf_slice, "[{}]:{}", self.ip(), self.port()).unwrap();
+            let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();
 
-        // This unsafe is OK because we know what is being written to the buffer
-        let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
-        f.pad(buf)
+            // This unsafe is OK because we know what is being written to the buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            f.pad(buf)
+        }
     }
 }
 
@@ -1193,6 +1205,28 @@ mod tests {
     }
 
     #[test]
+    fn socket_v4_to_str() {
+        let socket = SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 8080);
+
+        assert_eq!(format!("{}", socket), "192.168.0.1:8080");
+        assert_eq!(format!("{:<20}", socket), "192.168.0.1:8080    ");
+        assert_eq!(format!("{:>20}", socket), "    192.168.0.1:8080");
+        assert_eq!(format!("{:^20}", socket), "  192.168.0.1:8080  ");
+        assert_eq!(format!("{:.10}", socket), "192.168.0.");
+    }
+
+    #[test]
+    fn socket_v6_to_str() {
+        let socket: SocketAddrV6 = "[2a02:6b8:0:1::1]:53".parse().unwrap();
+
+        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::");
+    }
+
+    #[test]
     fn compare() {
         let v4_1 = "224.120.45.1:23456".parse::<SocketAddrV4>().unwrap();
         let v4_2 = "224.210.103.5:12345".parse::<SocketAddrV4>().unwrap();