Skip to content

Commit 033e9d6

Browse files
committed
Move IpDisplayBuffer into submodule.
1 parent 5a11b81 commit 033e9d6

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

library/std/src/net/ip.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,11 @@ mod tests;
55
use crate::cmp::Ordering;
66
use crate::fmt::{self, Write};
77
use crate::mem::transmute;
8-
use crate::str;
98
use crate::sys::net::netc as c;
109
use crate::sys_common::{FromInner, IntoInner};
1110

12-
/// Used for slow path in `Display` implementations when alignment is required.
13-
struct IpDisplayBuffer<const SIZE: usize> {
14-
buf: [u8; SIZE],
15-
len: usize,
16-
}
17-
18-
impl<const SIZE: usize> IpDisplayBuffer<SIZE> {
19-
#[inline(always)]
20-
pub const fn new(_ip: &[u8; SIZE]) -> Self {
21-
Self { buf: [0; SIZE], len: 0 }
22-
}
23-
24-
#[inline(always)]
25-
pub fn as_str(&self) -> &str {
26-
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
27-
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
28-
unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) }
29-
}
30-
}
31-
32-
impl<const SIZE: usize> fmt::Write for IpDisplayBuffer<SIZE> {
33-
fn write_str(&mut self, s: &str) -> fmt::Result {
34-
if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) {
35-
buf.copy_from_slice(s.as_bytes());
36-
self.len += s.len();
37-
Ok(())
38-
} else {
39-
Err(fmt::Error)
40-
}
41-
}
42-
}
11+
mod display_buffer;
12+
use display_buffer::IpDisplayBuffer;
4313

4414
/// An IP address, either IPv4 or IPv6.
4515
///
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::fmt;
2+
use crate::str;
3+
4+
/// Used for slow path in `Display` implementations when alignment is required.
5+
pub struct IpDisplayBuffer<const SIZE: usize> {
6+
buf: [u8; SIZE],
7+
len: usize,
8+
}
9+
10+
impl<const SIZE: usize> IpDisplayBuffer<SIZE> {
11+
#[inline(always)]
12+
pub const fn new(_ip: &[u8; SIZE]) -> Self {
13+
Self { buf: [0; SIZE], len: 0 }
14+
}
15+
16+
#[inline(always)]
17+
pub fn as_str(&self) -> &str {
18+
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
19+
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
20+
unsafe { str::from_utf8_unchecked(&self.buf[..self.len]) }
21+
}
22+
}
23+
24+
impl<const SIZE: usize> fmt::Write for IpDisplayBuffer<SIZE> {
25+
fn write_str(&mut self, s: &str) -> fmt::Result {
26+
if let Some(buf) = self.buf.get_mut(self.len..(self.len + s.len())) {
27+
buf.copy_from_slice(s.as_bytes());
28+
self.len += s.len();
29+
Ok(())
30+
} else {
31+
Err(fmt::Error)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)