Skip to content

Commit 44d6242

Browse files
committed
Simplify IpDisplayBuffer API.
1 parent 31540f5 commit 44d6242

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

library/std/src/net/ip.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,9 @@ impl fmt::Display for Ipv4Addr {
999999
if fmt.precision().is_none() && fmt.width().is_none() {
10001000
write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
10011001
} else {
1002-
let mut buf = IpDisplayBuffer::new(b"255.255.255.255");
1002+
const LONGEST_IPV4_ADDR: &str = "255.255.255.255";
1003+
1004+
let mut buf = IpDisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
10031005
// Buffer is long enough for the longest possible IPv4 address, so this should never fail.
10041006
write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
10051007

@@ -1778,7 +1780,9 @@ impl fmt::Display for Ipv6Addr {
17781780
}
17791781
}
17801782
} else {
1781-
let mut buf = IpDisplayBuffer::new(b"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
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();
17821786
// Buffer is long enough for the longest possible IPv6 address, so this should never fail.
17831787
write!(buf, "{}", self).unwrap();
17841788

library/std/src/net/ip/display_buffer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ pub struct IpDisplayBuffer<const SIZE: usize> {
99
}
1010

1111
impl<const SIZE: usize> IpDisplayBuffer<SIZE> {
12-
#[inline(always)]
13-
pub const fn new(_ip: &[u8; SIZE]) -> Self {
14-
Self { buf: MaybeUninit::uninit_array::<SIZE>(), len: 0 }
12+
#[inline]
13+
pub const fn new() -> Self {
14+
Self { buf: MaybeUninit::uninit_array(), len: 0 }
1515
}
1616

17-
#[inline(always)]
17+
#[inline]
1818
pub fn as_str(&self) -> &str {
1919
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
2020
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.

0 commit comments

Comments
 (0)