This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +14
-6
lines changed Expand file tree Collapse file tree 2 files changed +14
-6
lines changed Original file line number Diff line number Diff line change 294294#![ feature( std_internals) ]
295295#![ feature( str_internals) ]
296296#![ feature( strict_provenance) ]
297+ #![ feature( maybe_uninit_uninit_array) ]
298+ #![ feature( const_maybe_uninit_uninit_array) ]
297299//
298300// Library features (alloc):
299301#![ feature( alloc_layout_extra) ]
Original file line number Diff line number Diff line change 11use crate :: fmt;
2+ use crate :: mem:: MaybeUninit ;
23use crate :: str;
34
45/// Used for slow path in `Display` implementations when alignment is required.
56pub struct IpDisplayBuffer < const SIZE : usize > {
6- buf : [ u8 ; SIZE ] ,
7+ buf : [ MaybeUninit < u8 > ; SIZE ] ,
78 len : usize ,
89}
910
1011impl < const SIZE : usize > IpDisplayBuffer < SIZE > {
1112 #[ inline( always) ]
1213 pub const fn new ( _ip : & [ u8 ; SIZE ] ) -> Self {
13- Self { buf : [ 0 ; SIZE ] , len : 0 }
14+ Self { buf : MaybeUninit :: uninit_array :: < SIZE > ( ) , len : 0 }
1415 }
1516
1617 #[ inline( always) ]
1718 pub fn as_str ( & self ) -> & str {
1819 // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
1920 // 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+ }
2125 }
2226}
2327
2428impl < const SIZE : usize > fmt:: Write for IpDisplayBuffer < SIZE > {
2529 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 ( ) ;
2935 Ok ( ( ) )
3036 } else {
3137 Err ( fmt:: Error )
You can’t perform that action at this time.
0 commit comments