2
2
mod tests;
3
3
4
4
use crate :: cmp:: Ordering ;
5
- use crate :: fmt;
5
+ use crate :: fmt:: { self , Write } ;
6
6
use crate :: hash;
7
- use crate :: io:: { self , Write } ;
7
+ use crate :: io;
8
8
use crate :: iter;
9
9
use crate :: mem;
10
10
use crate :: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
@@ -15,6 +15,8 @@ use crate::sys_common::net::LookupHost;
15
15
use crate :: sys_common:: { FromInner , IntoInner } ;
16
16
use crate :: vec;
17
17
18
+ use super :: display_buffer:: DisplayBuffer ;
19
+
18
20
/// An internet socket address, either IPv4 or IPv6.
19
21
///
20
22
/// Internet socket addresses consist of an [IP address], a 16-bit port number, as well
@@ -616,25 +618,18 @@ impl fmt::Debug for SocketAddr {
616
618
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
617
619
impl fmt:: Display for SocketAddrV4 {
618
620
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
619
- // Fast path: if there's no alignment stuff , write to the output buffer
620
- // directly
621
+ // If there are no alignment requirements , write the socket address directly to `f`.
622
+ // Otherwise, write it to a local buffer and then use `f.pad`.
621
623
if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
622
624
write ! ( f, "{}:{}" , self . ip( ) , self . port( ) )
623
625
} else {
624
- const IPV4_SOCKET_BUF_LEN : usize = ( 3 * 4 ) // the segments
625
- + 3 // the separators
626
- + 1 + 5 ; // the port
627
- let mut buf = [ 0 ; IPV4_SOCKET_BUF_LEN ] ;
628
- let mut buf_slice = & mut buf[ ..] ;
629
-
630
- // Unwrap is fine because writing to a sufficiently-sized
631
- // buffer is infallible
632
- write ! ( buf_slice, "{}:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
633
- let len = IPV4_SOCKET_BUF_LEN - buf_slice. len ( ) ;
634
-
635
- // This unsafe is OK because we know what is being written to the buffer
636
- let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
637
- f. pad ( buf)
626
+ const LONGEST_IPV4_SOCKET_ADDR : & str = "255.255.255.255:65536" ;
627
+
628
+ let mut buf = DisplayBuffer :: < { LONGEST_IPV4_SOCKET_ADDR . len ( ) } > :: new ( ) ;
629
+ // Buffer is long enough for the longest possible IPv4 socket address, so this should never fail.
630
+ write ! ( buf, "{}:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
631
+
632
+ f. pad ( buf. as_str ( ) )
638
633
}
639
634
}
640
635
}
@@ -649,35 +644,26 @@ impl fmt::Debug for SocketAddrV4 {
649
644
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
650
645
impl fmt:: Display for SocketAddrV6 {
651
646
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
652
- // Fast path: if there's no alignment stuff , write to the output
653
- // buffer directly
647
+ // If there are no alignment requirements , write the socket address directly to `f`.
648
+ // Otherwise, write it to a local buffer and then use `f.pad`.
654
649
if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
655
650
match self . scope_id ( ) {
656
651
0 => write ! ( f, "[{}]:{}" , self . ip( ) , self . port( ) ) ,
657
652
scope_id => write ! ( f, "[{}%{}]:{}" , self . ip( ) , scope_id, self . port( ) ) ,
658
653
}
659
654
} else {
660
- const IPV6_SOCKET_BUF_LEN : usize = ( 4 * 8 ) // The address
661
- + 7 // The colon separators
662
- + 2 // The brackets
663
- + 1 + 10 // The scope id
664
- + 1 + 5 ; // The port
665
-
666
- let mut buf = [ 0 ; IPV6_SOCKET_BUF_LEN ] ;
667
- let mut buf_slice = & mut buf[ ..] ;
655
+ const LONGEST_IPV6_SOCKET_ADDR : & str =
656
+ "[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967296]:65536" ;
668
657
658
+ let mut buf = DisplayBuffer :: < { LONGEST_IPV6_SOCKET_ADDR . len ( ) } > :: new ( ) ;
669
659
match self . scope_id ( ) {
670
- 0 => write ! ( buf_slice , "[{}]:{}" , self . ip( ) , self . port( ) ) ,
671
- scope_id => write ! ( buf_slice , "[{}%{}]:{}" , self . ip( ) , scope_id, self . port( ) ) ,
660
+ 0 => write ! ( buf , "[{}]:{}" , self . ip( ) , self . port( ) ) ,
661
+ scope_id => write ! ( buf , "[{}%{}]:{}" , self . ip( ) , scope_id, self . port( ) ) ,
672
662
}
673
- // Unwrap is fine because writing to a sufficiently-sized
674
- // buffer is infallible
663
+ // Buffer is long enough for the longest possible IPv6 socket address, so this should never fail.
675
664
. unwrap ( ) ;
676
- let len = IPV6_SOCKET_BUF_LEN - buf_slice. len ( ) ;
677
665
678
- // This unsafe is OK because we know what is being written to the buffer
679
- let buf = unsafe { crate :: str:: from_utf8_unchecked ( & buf[ ..len] ) } ;
680
- f. pad ( buf)
666
+ f. pad ( buf. as_str ( ) )
681
667
}
682
668
}
683
669
}
0 commit comments