Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions library/core/src/net/display_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ use crate::mem::MaybeUninit;
use crate::{fmt, str};

/// Used for slow path in `Display` implementations when alignment is required.
pub(super) struct DisplayBuffer<const SIZE: usize> {
buf: [MaybeUninit<u8>; SIZE],
pub(super) struct DisplayBuffer<'a> {
buf: &'a mut [MaybeUninit<u8>],
len: usize,
}

impl<const SIZE: usize> DisplayBuffer<SIZE> {
impl<'a> DisplayBuffer<'a> {
#[inline]
pub(super) const fn new() -> Self {
Self { buf: [MaybeUninit::uninit(); SIZE], len: 0 }
pub(super) const fn new(buf: &'a mut [MaybeUninit<u8>]) -> Self {
Self { buf, len: 0 }
}

#[inline]
pub(super) const fn buffer<const SIZE: usize>() -> [MaybeUninit<u8>; SIZE] {
[MaybeUninit::uninit(); SIZE]
}

#[inline]
Expand All @@ -24,7 +29,7 @@ impl<const SIZE: usize> DisplayBuffer<SIZE> {
}
}

impl<const SIZE: usize> fmt::Write for DisplayBuffer<SIZE> {
impl fmt::Write for DisplayBuffer<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();

Expand Down
6 changes: 4 additions & 2 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,8 @@ impl fmt::Display for Ipv4Addr {
} else {
const LONGEST_IPV4_ADDR: &str = "255.255.255.255";

let mut buf = DisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV4_ADDR.len() }>();
let mut buf = DisplayBuffer::new(&mut buf);
// Buffer is long enough for the longest possible IPv4 address, so this should never fail.
write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();

Expand Down Expand Up @@ -2197,7 +2198,8 @@ impl fmt::Display for Ipv6Addr {
} else {
const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";

let mut buf = DisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();
let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV6_ADDR.len() }>();
let mut buf = DisplayBuffer::new(&mut buf);
// Buffer is long enough for the longest possible IPv6 address, so this should never fail.
write!(buf, "{}", self).unwrap();

Expand Down
6 changes: 4 additions & 2 deletions library/core/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ impl fmt::Display for SocketAddrV4 {
} else {
const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65535";

let mut buf = DisplayBuffer::<{ LONGEST_IPV4_SOCKET_ADDR.len() }>::new();
let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV4_SOCKET_ADDR.len() }>();
let mut buf = DisplayBuffer::new(&mut buf);
// Buffer is long enough for the longest possible IPv4 socket address, so this should never fail.
write!(buf, "{}:{}", self.ip(), self.port()).unwrap();

Expand Down Expand Up @@ -682,7 +683,8 @@ impl fmt::Display for SocketAddrV6 {
const LONGEST_IPV6_SOCKET_ADDR: &str =
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967295]:65535";

let mut buf = DisplayBuffer::<{ LONGEST_IPV6_SOCKET_ADDR.len() }>::new();
let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV6_SOCKET_ADDR.len() }>();
let mut buf = DisplayBuffer::new(&mut buf);
match self.scope_id() {
0 => write!(buf, "[{}]:{}", self.ip(), self.port()),
scope_id => write!(buf, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
Expand Down
Loading