22mod tests;
33
44use crate :: cmp:: Ordering ;
5- use crate :: fmt;
5+ use crate :: fmt:: { self , Write } ;
66use crate :: hash;
7- use crate :: io:: { self , Write } ;
7+ use crate :: io;
88use crate :: iter;
99use crate :: mem;
1010use crate :: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
@@ -15,6 +15,8 @@ use crate::sys_common::net::LookupHost;
1515use crate :: sys_common:: { FromInner , IntoInner } ;
1616use crate :: vec;
1717
18+ use super :: display_buffer:: DisplayBuffer ;
19+
1820/// An internet socket address, either IPv4 or IPv6.
1921///
2022/// Internet socket addresses consist of an [IP address], a 16-bit port number, as well
@@ -616,25 +618,18 @@ impl fmt::Debug for SocketAddr {
616618#[ stable( feature = "rust1" , since = "1.0.0" ) ]
617619impl fmt:: Display for SocketAddrV4 {
618620 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`.
621623 if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
622624 write ! ( f, "{}:{}" , self . ip( ) , self . port( ) )
623625 } 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 ( ) )
638633 }
639634 }
640635}
@@ -649,35 +644,26 @@ impl fmt::Debug for SocketAddrV4 {
649644#[ stable( feature = "rust1" , since = "1.0.0" ) ]
650645impl fmt:: Display for SocketAddrV6 {
651646 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`.
654649 if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
655650 match self . scope_id ( ) {
656651 0 => write ! ( f, "[{}]:{}" , self . ip( ) , self . port( ) ) ,
657652 scope_id => write ! ( f, "[{}%{}]:{}" , self . ip( ) , scope_id, self . port( ) ) ,
658653 }
659654 } 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" ;
668657
658+ let mut buf = DisplayBuffer :: < { LONGEST_IPV6_SOCKET_ADDR . len ( ) } > :: new ( ) ;
669659 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( ) ) ,
672662 }
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.
675664 . unwrap ( ) ;
676- let len = IPV6_SOCKET_BUF_LEN - buf_slice. len ( ) ;
677665
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 ( ) )
681667 }
682668 }
683669}
0 commit comments