@@ -600,17 +600,23 @@ impl fmt::Display for SocketAddr {
600
600
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
601
601
impl fmt:: Display for SocketAddrV4 {
602
602
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
603
- const IPV4_SOCKET_BUF_LEN : usize = 21 ;
604
- let mut buf = [ 0 ; IPV4_SOCKET_BUF_LEN ] ;
605
- let mut buf_slice = & mut buf[ ..] ;
606
-
607
- // Unwrap is fine because writing to a buffer is infallible
608
- write ! ( buf_slice, "{}:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
609
- let len = IPV4_SOCKET_BUF_LEN - buf_slice. len ( ) ;
610
-
611
- // This unsafe is OK because we know what is being written to the buffer
612
- let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
613
- f. pad ( buf)
603
+ // Fast path: if there's no alignment stuff, write to the output buffer
604
+ // directly
605
+ if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
606
+ write ! ( f, "{}:{}" , self . ip( ) , self . port( ) )
607
+ } else {
608
+ const IPV4_SOCKET_BUF_LEN : usize = 21 ;
609
+ let mut buf = [ 0 ; IPV4_SOCKET_BUF_LEN ] ;
610
+ let mut buf_slice = & mut buf[ ..] ;
611
+
612
+ // Unwrap is fine because writing to a buffer is infallible
613
+ write ! ( buf_slice, "{}:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
614
+ let len = IPV4_SOCKET_BUF_LEN - buf_slice. len ( ) ;
615
+
616
+ // This unsafe is OK because we know what is being written to the buffer
617
+ let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
618
+ f. pad ( buf)
619
+ }
614
620
}
615
621
}
616
622
@@ -624,21 +630,27 @@ impl fmt::Debug for SocketAddrV4 {
624
630
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
625
631
impl fmt:: Display for SocketAddrV6 {
626
632
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
627
- const IPV6_SOCKET_BUF_LEN : usize = ( 4 * 8 ) // The address
633
+ // Fast path: if there's no alignment stuff, write to the output
634
+ // buffer directly
635
+ if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
636
+ write ! ( f, "[{}]:{}" , self . ip( ) , self . port( ) )
637
+ } else {
638
+ const IPV6_SOCKET_BUF_LEN : usize = ( 4 * 8 ) // The address
628
639
+ 7 // The colon separators
629
640
+ 2 // The brackets
630
641
+ 1 + 5 ; // The port
631
642
632
- let mut buf = [ 0 ; IPV6_SOCKET_BUF_LEN ] ;
633
- let mut buf_slice = & mut buf[ ..] ;
643
+ let mut buf = [ 0 ; IPV6_SOCKET_BUF_LEN ] ;
644
+ let mut buf_slice = & mut buf[ ..] ;
634
645
635
- // Unwrap is fine because writing to a buffer is infallible
636
- write ! ( buf_slice, "[{}]:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
637
- let len = IPV6_SOCKET_BUF_LEN - buf_slice. len ( ) ;
646
+ // Unwrap is fine because writing to a buffer is infallible
647
+ write ! ( buf_slice, "[{}]:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
648
+ let len = IPV6_SOCKET_BUF_LEN - buf_slice. len ( ) ;
638
649
639
- // This unsafe is OK because we know what is being written to the buffer
640
- let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
641
- f. pad ( buf)
650
+ // This unsafe is OK because we know what is being written to the buffer
651
+ let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
652
+ f. pad ( buf)
653
+ }
642
654
}
643
655
}
644
656
@@ -1192,6 +1204,28 @@ mod tests {
1192
1204
assert ! ( v6. is_ipv6( ) ) ;
1193
1205
}
1194
1206
1207
+ #[ test]
1208
+ fn socket_v4_to_str ( ) {
1209
+ let socket = SocketAddrV4 :: new ( Ipv4Addr :: new ( 192 , 168 , 0 , 1 ) , 8080 ) ;
1210
+
1211
+ assert_eq ! ( format!( "{}" , socket) , "192.168.0.1:8080" ) ;
1212
+ assert_eq ! ( format!( "{:<20}" , socket) , "192.168.0.1:8080 " ) ;
1213
+ assert_eq ! ( format!( "{:>20}" , socket) , " 192.168.0.1:8080" ) ;
1214
+ assert_eq ! ( format!( "{:^20}" , socket) , " 192.168.0.1:8080 " ) ;
1215
+ assert_eq ! ( format!( "{:.10}" , socket) , "192.168.0." ) ;
1216
+ }
1217
+
1218
+ #[ test]
1219
+ fn socket_v6_to_str ( ) {
1220
+ let socket: SocketAddrV6 = "[2a02:6b8:0:1::1]:53" . parse ( ) . unwrap ( ) ;
1221
+
1222
+ assert_eq ! ( format!( "{}" , socket) , "[2a02:6b8:0:1::1]:53" ) ;
1223
+ assert_eq ! ( format!( "{:<24}" , socket) , "[2a02:6b8:0:1::1]:53 " ) ;
1224
+ assert_eq ! ( format!( "{:>24}" , socket) , " [2a02:6b8:0:1::1]:53" ) ;
1225
+ assert_eq ! ( format!( "{:^24}" , socket) , " [2a02:6b8:0:1::1]:53 " ) ;
1226
+ assert_eq ! ( format!( "{:.15}" , socket) , "[2a02:6b8:0:1::" ) ;
1227
+ }
1228
+
1195
1229
#[ test]
1196
1230
fn compare ( ) {
1197
1231
let v4_1 = "224.120.45.1:23456" . parse :: < SocketAddrV4 > ( ) . unwrap ( ) ;
0 commit comments