@@ -208,7 +208,11 @@ macro_rules! impl_Display {
208
208
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
209
209
#[ cfg( not( feature = "optimize_for_size" ) ) ]
210
210
{
211
- self . _fmt( true , f)
211
+ const MAX_DEC_N : usize = $unsigned:: MAX . ilog( 10 ) as usize + 1 ;
212
+ // Buffer decimals for $unsigned with right alignment.
213
+ let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
214
+
215
+ f. pad_integral( true , "" , self . _fmt( & mut buf) )
212
216
}
213
217
#[ cfg( feature = "optimize_for_size" ) ]
214
218
{
@@ -222,7 +226,11 @@ macro_rules! impl_Display {
222
226
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
223
227
#[ cfg( not( feature = "optimize_for_size" ) ) ]
224
228
{
225
- return self . unsigned_abs( ) . _fmt( * self >= 0 , f) ;
229
+ const MAX_DEC_N : usize = $unsigned:: MAX . ilog( 10 ) as usize + 1 ;
230
+ // Buffer decimals for $unsigned with right alignment.
231
+ let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
232
+
233
+ f. pad_integral( * self >= 0 , "" , self . unsigned_abs( ) . _fmt( & mut buf) )
226
234
}
227
235
#[ cfg( feature = "optimize_for_size" ) ]
228
236
{
@@ -233,10 +241,13 @@ macro_rules! impl_Display {
233
241
234
242
#[ cfg( not( feature = "optimize_for_size" ) ) ]
235
243
impl $unsigned {
236
- fn _fmt( self , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
237
- const MAX_DEC_N : usize = $unsigned:: MAX . ilog( 10 ) as usize + 1 ;
238
- // Buffer decimals for $unsigned with right alignment.
239
- let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
244
+ #[ doc( hidden) ]
245
+ #[ unstable(
246
+ feature = "fmt_internals" ,
247
+ reason = "internal routines only exposed for testing" ,
248
+ issue = "none"
249
+ ) ]
250
+ pub fn _fmt<' a>( self , buf: & ' a mut [ MaybeUninit :: <u8 >] ) -> & ' a str {
240
251
// Count the number of bytes in buf that are not initialized.
241
252
let mut offset = buf. len( ) ;
242
253
// Consume the least-significant decimals from a working copy.
@@ -301,13 +312,12 @@ macro_rules! impl_Display {
301
312
// SAFETY: All buf content since offset is set.
302
313
let written = unsafe { buf. get_unchecked( offset..) } ;
303
314
// SAFETY: Writes use ASCII from the lookup table exclusively.
304
- let as_str = unsafe {
315
+ unsafe {
305
316
str :: from_utf8_unchecked( slice:: from_raw_parts(
306
317
MaybeUninit :: slice_as_ptr( written) ,
307
318
written. len( ) ,
308
319
) )
309
- } ;
310
- f. pad_integral( is_nonnegative, "" , as_str)
320
+ }
311
321
}
312
322
} ) *
313
323
0 commit comments