@@ -6,7 +6,7 @@ use ops::{Div, Rem, Sub};
66use str;
77use slice;
88use ptr;
9- use mem;
9+ use mem:: MaybeUninit ;
1010
1111#[ doc( hidden) ]
1212trait Int : PartialEq + PartialOrd + Div < Output =Self > + Rem < Output =Self > +
@@ -51,7 +51,12 @@ trait GenericRadix {
5151 // characters for a base 2 number.
5252 let zero = T :: zero ( ) ;
5353 let is_nonnegative = x >= zero;
54- let mut buf: [ u8 ; 128 ] = unsafe { mem:: uninitialized ( ) } ;
54+ // Creating a `[MaybeUninit; N]` array by first creating a
55+ // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
56+ // array does not require initialization.
57+ let mut buf: [ MaybeUninit < u8 > ; 128 ] = unsafe {
58+ MaybeUninit :: uninitialized ( ) . into_inner ( )
59+ } ;
5560 let mut curr = buf. len ( ) ;
5661 let base = T :: from_u8 ( Self :: BASE ) ;
5762 if is_nonnegative {
@@ -60,7 +65,7 @@ trait GenericRadix {
6065 for byte in buf. iter_mut ( ) . rev ( ) {
6166 let n = x % base; // Get the current place value.
6267 x = x / base; // Deaccumulate the number.
63- * byte = Self :: digit ( n. to_u8 ( ) ) ; // Store the digit in the buffer.
68+ byte. set ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
6469 curr -= 1 ;
6570 if x == zero {
6671 // No more digits left to accumulate.
@@ -72,15 +77,19 @@ trait GenericRadix {
7277 for byte in buf. iter_mut ( ) . rev ( ) {
7378 let n = zero - ( x % base) ; // Get the current place value.
7479 x = x / base; // Deaccumulate the number.
75- * byte = Self :: digit ( n. to_u8 ( ) ) ; // Store the digit in the buffer.
80+ byte. set ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
7681 curr -= 1 ;
7782 if x == zero {
7883 // No more digits left to accumulate.
7984 break
8085 } ;
8186 }
8287 }
83- let buf = unsafe { str:: from_utf8_unchecked ( & buf[ curr..] ) } ;
88+ let buf = & buf[ curr..] ;
89+ let buf = unsafe { str:: from_utf8_unchecked ( slice:: from_raw_parts (
90+ MaybeUninit :: first_ptr ( buf) ,
91+ buf. len ( )
92+ ) ) } ;
8493 f. pad_integral ( is_nonnegative, Self :: PREFIX , buf)
8594 }
8695}
@@ -194,9 +203,14 @@ macro_rules! impl_Display {
194203 // convert the negative num to positive by summing 1 to it's 2 complement
195204 ( !self . $conv_fn( ) ) . wrapping_add( 1 )
196205 } ;
197- let mut buf: [ u8 ; 39 ] = unsafe { mem:: uninitialized( ) } ;
206+ // Creating a `[MaybeUninit; N]` array by first creating a
207+ // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
208+ // array does not require initialization.
209+ let mut buf: [ MaybeUninit <u8 >; 39 ] = unsafe {
210+ MaybeUninit :: uninitialized( ) . into_inner( )
211+ } ;
198212 let mut curr = buf. len( ) as isize ;
199- let buf_ptr = buf . as_mut_ptr ( ) ;
213+ let buf_ptr = MaybeUninit :: first_mut_ptr ( & mut buf ) ;
200214 let lut_ptr = DEC_DIGITS_LUT . as_ptr( ) ;
201215
202216 unsafe {
0 commit comments