diff options
| author | Markus Reiter <me@reitermark.us> | 2022-08-16 17:48:55 +0200 |
|---|---|---|
| committer | Markus Reiter <me@reitermark.us> | 2022-08-16 17:57:46 +0200 |
| commit | 033e9d66ffa918b2cd78649c5b8cba372f58f9e4 (patch) | |
| tree | e9793abdb34399e2c68cb5d662beb61f36bcf159 | |
| parent | 5a11b814d4cd84d43f7b96a81f3e1595187c26a1 (diff) | |
| download | rust-033e9d66ffa918b2cd78649c5b8cba372f58f9e4.tar.gz rust-033e9d66ffa918b2cd78649c5b8cba372f58f9e4.zip | |
Move `IpDisplayBuffer` into submodule.
| -rw-r--r-- | library/std/src/net/ip.rs | 34 | ||||
| -rw-r--r-- | library/std/src/net/ip/display_buffer.rs | 34 |
2 files changed, 36 insertions, 32 deletions
diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index f4fba7f7386..198dcff12b9 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -5,41 +5,11 @@ mod tests; use crate::cmp::Ordering; use crate::fmt::{self, Write}; use crate::mem::transmute; -use crate::str; use crate::sys::net::netc as c; use crate::sys_common::{FromInner, IntoInner}; -/// Used for slow path in `Display` implementations when alignment is required. -struct IpDisplayBuffer<const SIZE: usize> { - buf: [u8; SIZE], - len: usize, -} - -impl<const SIZE: usize> IpDisplayBuffer<SIZE> { - #[inline(always)] - pub const fn new(_ip: &[u8; SIZE]) -> Self { - Self { buf: [0; SIZE], len: 0 } - } - - #[inline(always)] - pub fn as_str(&self) -> &str { - // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation - // which writes a valid UTF-8 string to `buf` and correctly sets `len`. - unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } - } -} - -impl<const SIZE: usize> fmt::Write for IpDisplayBuffer<SIZE> { - fn write_str(&mut self, s: &str) -> fmt::Result { - if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { - buf.copy_from_slice(s.as_bytes()); - self.len += s.len(); - Ok(()) - } else { - Err(fmt::Error) - } - } -} +mod display_buffer; +use display_buffer::IpDisplayBuffer; /// An IP address, either IPv4 or IPv6. /// diff --git a/library/std/src/net/ip/display_buffer.rs b/library/std/src/net/ip/display_buffer.rs new file mode 100644 index 00000000000..06acfa5e215 --- /dev/null +++ b/library/std/src/net/ip/display_buffer.rs @@ -0,0 +1,34 @@ +use crate::fmt; +use crate::str; + +/// Used for slow path in `Display` implementations when alignment is required. +pub struct IpDisplayBuffer<const SIZE: usize> { + buf: [u8; SIZE], + len: usize, +} + +impl<const SIZE: usize> IpDisplayBuffer<SIZE> { + #[inline(always)] + pub const fn new(_ip: &[u8; SIZE]) -> Self { + Self { buf: [0; SIZE], len: 0 } + } + + #[inline(always)] + pub fn as_str(&self) -> &str { + // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation + // which writes a valid UTF-8 string to `buf` and correctly sets `len`. + unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) } + } +} + +impl<const SIZE: usize> fmt::Write for IpDisplayBuffer<SIZE> { + fn write_str(&mut self, s: &str) -> fmt::Result { + if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) { + buf.copy_from_slice(s.as_bytes()); + self.len += s.len(); + Ok(()) + } else { + Err(fmt::Error) + } + } +} |
