@@ -6,7 +6,7 @@ use ops::{Div, Rem, Sub};
6
6
use str;
7
7
use slice;
8
8
use ptr;
9
- use mem;
9
+ use mem:: MaybeUninit ;
10
10
11
11
#[ doc( hidden) ]
12
12
trait Int : PartialEq + PartialOrd + Div < Output =Self > + Rem < Output =Self > +
@@ -51,7 +51,12 @@ trait GenericRadix {
51
51
// characters for a base 2 number.
52
52
let zero = T :: zero ( ) ;
53
53
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
+ } ;
55
60
let mut curr = buf. len ( ) ;
56
61
let base = T :: from_u8 ( Self :: BASE ) ;
57
62
if is_nonnegative {
@@ -60,7 +65,7 @@ trait GenericRadix {
60
65
for byte in buf. iter_mut ( ) . rev ( ) {
61
66
let n = x % base; // Get the current place value.
62
67
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.
64
69
curr -= 1 ;
65
70
if x == zero {
66
71
// No more digits left to accumulate.
@@ -72,15 +77,19 @@ trait GenericRadix {
72
77
for byte in buf. iter_mut ( ) . rev ( ) {
73
78
let n = zero - ( x % base) ; // Get the current place value.
74
79
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.
76
81
curr -= 1 ;
77
82
if x == zero {
78
83
// No more digits left to accumulate.
79
84
break
80
85
} ;
81
86
}
82
87
}
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
+ ) ) } ;
84
93
f. pad_integral ( is_nonnegative, Self :: PREFIX , buf)
85
94
}
86
95
}
@@ -194,9 +203,14 @@ macro_rules! impl_Display {
194
203
// convert the negative num to positive by summing 1 to it's 2 complement
195
204
( !self . $conv_fn( ) ) . wrapping_add( 1 )
196
205
} ;
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
+ } ;
198
212
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 ) ;
200
214
let lut_ptr = DEC_DIGITS_LUT . as_ptr( ) ;
201
215
202
216
unsafe {
0 commit comments