File tree 2 files changed +14
-6
lines changed
2 files changed +14
-6
lines changed Original file line number Diff line number Diff line change 294
294
#![ feature( std_internals) ]
295
295
#![ feature( str_internals) ]
296
296
#![ feature( strict_provenance) ]
297
+ #![ feature( maybe_uninit_uninit_array) ]
298
+ #![ feature( const_maybe_uninit_uninit_array) ]
297
299
//
298
300
// Library features (alloc):
299
301
#![ feature( alloc_layout_extra) ]
Original file line number Diff line number Diff line change 1
1
use crate :: fmt;
2
+ use crate :: mem:: MaybeUninit ;
2
3
use crate :: str;
3
4
4
5
/// Used for slow path in `Display` implementations when alignment is required.
5
6
pub struct IpDisplayBuffer < const SIZE : usize > {
6
- buf : [ u8 ; SIZE ] ,
7
+ buf : [ MaybeUninit < u8 > ; SIZE ] ,
7
8
len : usize ,
8
9
}
9
10
10
11
impl < const SIZE : usize > IpDisplayBuffer < SIZE > {
11
12
#[ inline( always) ]
12
13
pub const fn new ( _ip : & [ u8 ; SIZE ] ) -> Self {
13
- Self { buf : [ 0 ; SIZE ] , len : 0 }
14
+ Self { buf : MaybeUninit :: uninit_array :: < SIZE > ( ) , len : 0 }
14
15
}
15
16
16
17
#[ inline( always) ]
17
18
pub fn as_str ( & self ) -> & str {
18
19
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
19
20
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
20
- unsafe { str:: from_utf8_unchecked ( & self . buf [ ..self . len ] ) }
21
+ unsafe {
22
+ let s = MaybeUninit :: slice_assume_init_ref ( & self . buf [ ..self . len ] ) ;
23
+ str:: from_utf8_unchecked ( s)
24
+ }
21
25
}
22
26
}
23
27
24
28
impl < const SIZE : usize > fmt:: Write for IpDisplayBuffer < SIZE > {
25
29
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 ( ) ;
30
+ let bytes = s. as_bytes ( ) ;
31
+
32
+ if let Some ( buf) = self . buf . get_mut ( self . len ..( self . len + bytes. len ( ) ) ) {
33
+ MaybeUninit :: write_slice ( buf, bytes) ;
34
+ self . len += bytes. len ( ) ;
29
35
Ok ( ( ) )
30
36
} else {
31
37
Err ( fmt:: Error )
You can’t perform that action at this time.
0 commit comments