File tree 2 files changed +36
-32
lines changed
2 files changed +36
-32
lines changed Original file line number Diff line number Diff line change @@ -5,41 +5,11 @@ mod tests;
5
5
use crate :: cmp:: Ordering ;
6
6
use crate :: fmt:: { self , Write } ;
7
7
use crate :: mem:: transmute;
8
- use crate :: str;
9
8
use crate :: sys:: net:: netc as c;
10
9
use crate :: sys_common:: { FromInner , IntoInner } ;
11
10
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 ;
43
13
44
14
/// An IP address, either IPv4 or IPv6.
45
15
///
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments