2
2
3
3
use crate :: mem:: MaybeUninit ;
4
4
use crate :: num:: fmt as numfmt;
5
- use crate :: ops:: { Div , Rem , Sub } ;
6
5
use crate :: { fmt, ptr, slice, str} ;
7
6
8
- #[ doc( hidden) ]
9
- trait DisplayInt :
10
- PartialEq + PartialOrd + Div < Output = Self > + Rem < Output = Self > + Sub < Output = Self > + Copy
11
- {
12
- #[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
13
- fn to_u32 ( & self ) -> u32 ;
14
- fn to_u64 ( & self ) -> u64 ;
15
- fn to_u128 ( & self ) -> u128 ;
16
- }
17
-
18
- macro_rules! impl_int {
19
- ( $( $t: ident) * ) => (
20
- $( impl DisplayInt for $t {
21
- #[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
22
- fn to_u32( & self ) -> u32 { * self as u32 }
23
- fn to_u64( & self ) -> u64 { * self as u64 }
24
- fn to_u128( & self ) -> u128 { * self as u128 }
25
- } ) *
26
- )
27
- }
28
-
29
- impl_int ! {
30
- i8 i16 i32 i64 i128 isize
31
- u8 u16 u32 u64 u128 usize
32
- }
33
-
34
7
// Formatting of integers with a non-decimal radix.
35
8
macro_rules! radix_integer {
36
9
( fmt:: $Trait: ident for $Signed: ident and $Unsigned: ident, $prefix: expr, $dig_tab: expr) => {
@@ -142,47 +115,47 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"\
142
115
8081828384858687888990919293949596979899";
143
116
144
117
macro_rules! impl_Display {
145
- ( $( $signed : ident, $unsigned : ident, ) * ; as $u : ident via $conv_fn : ident named $gen_name : ident) => {
118
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
146
119
147
120
$(
148
121
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
149
- impl fmt:: Display for $unsigned {
122
+ impl fmt:: Display for $Unsigned {
150
123
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
151
124
#[ cfg( not( feature = "optimize_for_size" ) ) ]
152
125
{
153
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
154
- // Buffer decimals for $unsigned with right alignment.
126
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
127
+ // Buffer decimals for self with right alignment.
155
128
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
156
129
157
130
f. pad_integral( true , "" , self . _fmt( & mut buf) )
158
131
}
159
132
#[ cfg( feature = "optimize_for_size" ) ]
160
133
{
161
- $gen_name ( self . $conv_fn ( ) , true , f)
134
+ $fmt_fn ( self as $T , true , f)
162
135
}
163
136
}
164
137
}
165
138
166
139
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
167
- impl fmt:: Display for $signed {
140
+ impl fmt:: Display for $Signed {
168
141
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
169
142
#[ cfg( not( feature = "optimize_for_size" ) ) ]
170
143
{
171
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
172
- // Buffer decimals for $unsigned with right alignment.
144
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
145
+ // Buffer decimals for self with right alignment.
173
146
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
174
147
175
148
f. pad_integral( * self >= 0 , "" , self . unsigned_abs( ) . _fmt( & mut buf) )
176
149
}
177
150
#[ cfg( feature = "optimize_for_size" ) ]
178
151
{
179
- return $gen_name ( self . unsigned_abs( ) . $conv_fn ( ) , * self >= 0 , f) ;
152
+ return $fmt_fn ( self . unsigned_abs( ) as $T , * self >= 0 , f) ;
180
153
}
181
154
}
182
155
}
183
156
184
157
#[ cfg( not( feature = "optimize_for_size" ) ) ]
185
- impl $unsigned {
158
+ impl $Unsigned {
186
159
#[ doc( hidden) ]
187
160
#[ unstable(
188
161
feature = "fmt_internals" ,
@@ -264,8 +237,8 @@ macro_rules! impl_Display {
264
237
} ) *
265
238
266
239
#[ cfg( feature = "optimize_for_size" ) ]
267
- fn $gen_name ( mut n: $u , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
268
- const MAX_DEC_N : usize = $u :: MAX . ilog10( ) as usize + 1 ;
240
+ fn $fmt_fn ( mut n: $T , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
241
+ const MAX_DEC_N : usize = $T :: MAX . ilog10( ) as usize + 1 ;
269
242
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
270
243
let mut curr = MAX_DEC_N ;
271
244
let buf_ptr = MaybeUninit :: slice_as_mut_ptr( & mut buf) ;
@@ -298,9 +271,9 @@ macro_rules! impl_Display {
298
271
}
299
272
300
273
macro_rules! impl_Exp {
301
- ( $( $t : ident) , * as $u : ident via $conv_fn : ident named $name : ident) => {
302
- fn $name (
303
- mut n: $u ,
274
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
275
+ fn $fmt_fn (
276
+ mut n: $T ,
304
277
is_nonnegative: bool ,
305
278
upper: bool ,
306
279
f: & mut fmt:: Formatter <' _>
@@ -434,32 +407,41 @@ macro_rules! impl_Exp {
434
407
435
408
$(
436
409
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
437
- impl fmt:: LowerExp for $t {
438
- #[ allow( unused_comparisons) ]
410
+ impl fmt:: LowerExp for $Signed {
439
411
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
440
412
let is_nonnegative = * self >= 0 ;
441
413
let n = if is_nonnegative {
442
- self . $conv_fn ( )
414
+ * self as $T
443
415
} else {
444
- // convert the negative num to positive by summing 1 to its 2s complement
445
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
416
+ self . unsigned_abs( ) as $T
446
417
} ;
447
- $name( n, is_nonnegative, false , f)
418
+ $fmt_fn( n, is_nonnegative, false , f)
419
+ }
420
+ }
421
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
422
+ impl fmt:: LowerExp for $Unsigned {
423
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
424
+ $fmt_fn( * self as $T, true , false , f)
448
425
}
449
426
} ) *
427
+
450
428
$(
451
429
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
452
- impl fmt:: UpperExp for $t {
453
- #[ allow( unused_comparisons) ]
430
+ impl fmt:: UpperExp for $Signed {
454
431
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
455
432
let is_nonnegative = * self >= 0 ;
456
433
let n = if is_nonnegative {
457
- self . $conv_fn ( )
434
+ * self as $T
458
435
} else {
459
- // convert the negative num to positive by summing 1 to its 2s complement
460
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
436
+ self . unsigned_abs( ) as $T
461
437
} ;
462
- $name( n, is_nonnegative, true , f)
438
+ $fmt_fn( n, is_nonnegative, true , f)
439
+ }
440
+ }
441
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
442
+ impl fmt:: UpperExp for $Unsigned {
443
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
444
+ $fmt_fn( * self as $T, true , true , f)
463
445
}
464
446
} ) *
465
447
} ;
@@ -475,37 +457,20 @@ impl_Debug! {
475
457
#[ cfg( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ]
476
458
mod imp {
477
459
use super :: * ;
478
- impl_Display ! (
479
- i8 , u8 ,
480
- i16 , u16 ,
481
- i32 , u32 ,
482
- i64 , u64 ,
483
- isize , usize ,
484
- ; as u64 via to_u64 named fmt_u64
485
- ) ;
486
- impl_Exp ! (
487
- i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , usize , isize
488
- as u64 via to_u64 named exp_u64
489
- ) ;
460
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into fmt_u64) ;
461
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into exp_u64) ;
490
462
}
491
463
492
464
#[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
493
465
mod imp {
494
466
use super :: * ;
495
- impl_Display ! (
496
- i8 , u8 ,
497
- i16 , u16 ,
498
- i32 , u32 ,
499
- isize , usize ,
500
- ; as u32 via to_u32 named fmt_u32) ;
501
- impl_Display ! (
502
- i64 , u64 ,
503
- ; as u64 via to_u64 named fmt_u64) ;
504
-
505
- impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize as u32 via to_u32 named exp_u32) ;
506
- impl_Exp ! ( i64 , u64 as u64 via to_u64 named exp_u64) ;
467
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into fmt_u32) ;
468
+ impl_Display ! ( i64 , u64 , ; as u64 into fmt_u64) ;
469
+
470
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into exp_u32) ;
471
+ impl_Exp ! ( i64 , u64 ; as u64 into exp_u64) ;
507
472
}
508
- impl_Exp ! ( i128 , u128 as u128 via to_u128 named exp_u128) ;
473
+ impl_Exp ! ( i128 , u128 ; as u128 into exp_u128) ;
509
474
510
475
const U128_MAX_DEC_N : usize = u128:: MAX . ilog10 ( ) as usize + 1 ;
511
476
0 commit comments