| 
3 | 3 | mod tests;  | 
4 | 4 | 
 
  | 
5 | 5 | use crate::cmp::Ordering;  | 
6 |  | -use crate::fmt::{self, Write as FmtWrite};  | 
7 |  | -use crate::io::Write as IoWrite;  | 
 | 6 | +use crate::fmt::{self, Write};  | 
8 | 7 | use crate::mem::transmute;  | 
9 | 8 | use crate::sys::net::netc as c;  | 
10 | 9 | use crate::sys_common::{FromInner, IntoInner};  | 
11 | 10 | 
 
  | 
 | 11 | +mod display_buffer;  | 
 | 12 | +use display_buffer::IpDisplayBuffer;  | 
 | 13 | + | 
12 | 14 | /// An IP address, either IPv4 or IPv6.  | 
13 | 15 | ///  | 
14 | 16 | /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their  | 
@@ -991,21 +993,19 @@ impl From<Ipv6Addr> for IpAddr {  | 
991 | 993 | impl fmt::Display for Ipv4Addr {  | 
992 | 994 |     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {  | 
993 | 995 |         let octets = self.octets();  | 
994 |  | -        // Fast Path: if there's no alignment stuff, write directly to the buffer  | 
 | 996 | + | 
 | 997 | +        // If there are no alignment requirements, write the IP address directly to `f`.  | 
 | 998 | +        // Otherwise, write it to a local buffer and then use `f.pad`.  | 
995 | 999 |         if fmt.precision().is_none() && fmt.width().is_none() {  | 
996 | 1000 |             write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])  | 
997 | 1001 |         } else {  | 
998 |  | -            const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address  | 
999 |  | -            let mut buf = [0u8; IPV4_BUF_LEN];  | 
1000 |  | -            let mut buf_slice = &mut buf[..];  | 
 | 1002 | +            const LONGEST_IPV4_ADDR: &str = "255.255.255.255";  | 
1001 | 1003 | 
 
  | 
1002 |  | -            // Note: The call to write should never fail, hence the unwrap  | 
1003 |  | -            write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();  | 
1004 |  | -            let len = IPV4_BUF_LEN - buf_slice.len();  | 
 | 1004 | +            let mut buf = IpDisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();  | 
 | 1005 | +            // Buffer is long enough for the longest possible IPv4 address, so this should never fail.  | 
 | 1006 | +            write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();  | 
1005 | 1007 | 
 
  | 
1006 |  | -            // This unsafe is OK because we know what is being written to the buffer  | 
1007 |  | -            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };  | 
1008 |  | -            fmt.pad(buf)  | 
 | 1008 | +            fmt.pad(buf.as_str())  | 
1009 | 1009 |         }  | 
1010 | 1010 |     }  | 
1011 | 1011 | }  | 
@@ -1708,8 +1708,8 @@ impl Ipv6Addr {  | 
1708 | 1708 | #[stable(feature = "rust1", since = "1.0.0")]  | 
1709 | 1709 | impl fmt::Display for Ipv6Addr {  | 
1710 | 1710 |     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {  | 
1711 |  | -        // If there are no alignment requirements, write out the IP address to  | 
1712 |  | -        // f. Otherwise, write it to a local buffer, then use f.pad.  | 
 | 1711 | +        // If there are no alignment requirements, write the IP address directly to `f`.  | 
 | 1712 | +        // Otherwise, write it to a local buffer and then use `f.pad`.  | 
1713 | 1713 |         if f.precision().is_none() && f.width().is_none() {  | 
1714 | 1714 |             let segments = self.segments();  | 
1715 | 1715 | 
 
  | 
@@ -1780,22 +1780,13 @@ impl fmt::Display for Ipv6Addr {  | 
1780 | 1780 |                 }  | 
1781 | 1781 |             }  | 
1782 | 1782 |         } else {  | 
1783 |  | -            // Slow path: write the address to a local buffer, then use f.pad.  | 
1784 |  | -            // Defined recursively by using the fast path to write to the  | 
1785 |  | -            // buffer.  | 
1786 |  | - | 
1787 |  | -            // This is the largest possible size of an IPv6 address  | 
1788 |  | -            const IPV6_BUF_LEN: usize = (4 * 8) + 7;  | 
1789 |  | -            let mut buf = [0u8; IPV6_BUF_LEN];  | 
1790 |  | -            let mut buf_slice = &mut buf[..];  | 
1791 |  | - | 
1792 |  | -            // Note: This call to write should never fail, so unwrap is okay.  | 
1793 |  | -            write!(buf_slice, "{}", self).unwrap();  | 
1794 |  | -            let len = IPV6_BUF_LEN - buf_slice.len();  | 
1795 |  | - | 
1796 |  | -            // This is safe because we know exactly what can be in this buffer  | 
1797 |  | -            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };  | 
1798 |  | -            f.pad(buf)  | 
 | 1783 | +            const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";  | 
 | 1784 | + | 
 | 1785 | +            let mut buf = IpDisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();  | 
 | 1786 | +            // Buffer is long enough for the longest possible IPv6 address, so this should never fail.  | 
 | 1787 | +            write!(buf, "{}", self).unwrap();  | 
 | 1788 | + | 
 | 1789 | +            f.pad(buf.as_str())  | 
1799 | 1790 |         }  | 
1800 | 1791 |     }  | 
1801 | 1792 | }  | 
 | 
0 commit comments