3
3
use crate :: fmt:: NumBuffer ;
4
4
use crate :: mem:: MaybeUninit ;
5
5
use crate :: num:: fmt as numfmt;
6
- use crate :: ops:: { Div , Rem , Sub } ;
7
6
use crate :: { fmt, ptr, slice, str} ;
8
7
9
- #[ doc( hidden) ]
10
- trait DisplayInt :
11
- PartialEq + PartialOrd + Div < Output = Self > + Rem < Output = Self > + Sub < Output = Self > + Copy
12
- {
13
- #[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
14
- fn to_u32 ( & self ) -> u32 ;
15
- fn to_u64 ( & self ) -> u64 ;
16
- fn to_u128 ( & self ) -> u128 ;
17
- }
18
-
19
- macro_rules! impl_int {
20
- ( $( $t: ident) * ) => (
21
- $( impl DisplayInt for $t {
22
- #[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
23
- fn to_u32( & self ) -> u32 { * self as u32 }
24
- fn to_u64( & self ) -> u64 { * self as u64 }
25
- fn to_u128( & self ) -> u128 { * self as u128 }
26
- } ) *
27
- )
28
- }
29
-
30
- impl_int ! {
31
- i8 i16 i32 i64 i128 isize
32
- u8 u16 u32 u64 u128 usize
33
- }
34
-
35
8
/// Formatting of integers with a non-decimal radix.
36
9
macro_rules! radix_integer {
37
10
( fmt:: $Trait: ident for $Signed: ident and $Unsigned: ident, $prefix: expr, $dig_tab: expr) => {
@@ -149,49 +122,49 @@ unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
149
122
}
150
123
151
124
macro_rules! impl_Display {
152
- ( $( $signed : ident, $unsigned : ident, ) * ; as $u : ident via $conv_fn : ident named $gen_name : ident) => {
125
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
153
126
154
127
$(
155
128
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
156
- impl fmt:: Display for $unsigned {
129
+ impl fmt:: Display for $Unsigned {
157
130
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
158
131
#[ cfg( not( feature = "optimize_for_size" ) ) ]
159
132
{
160
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
161
- // Buffer decimals for $unsigned with right alignment.
133
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
134
+ // Buffer decimals for self with right alignment.
162
135
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
163
136
164
137
// SAFETY: `buf` is always big enough to contain all the digits.
165
138
unsafe { f. pad_integral( true , "" , self . _fmt( & mut buf) ) }
166
139
}
167
140
#[ cfg( feature = "optimize_for_size" ) ]
168
141
{
169
- $gen_name ( self . $conv_fn ( ) , true , f)
142
+ $fmt_fn ( self as $T , true , f)
170
143
}
171
144
}
172
145
}
173
146
174
147
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
175
- impl fmt:: Display for $signed {
148
+ impl fmt:: Display for $Signed {
176
149
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
177
150
#[ cfg( not( feature = "optimize_for_size" ) ) ]
178
151
{
179
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
180
- // Buffer decimals for $unsigned with right alignment.
152
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
153
+ // Buffer decimals for self with right alignment.
181
154
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
182
155
183
156
// SAFETY: `buf` is always big enough to contain all the digits.
184
157
unsafe { f. pad_integral( * self >= 0 , "" , self . unsigned_abs( ) . _fmt( & mut buf) ) }
185
158
}
186
159
#[ cfg( feature = "optimize_for_size" ) ]
187
160
{
188
- return $gen_name ( self . unsigned_abs( ) . $conv_fn ( ) , * self >= 0 , f) ;
161
+ return $fmt_fn ( self . unsigned_abs( ) as $T , * self >= 0 , f) ;
189
162
}
190
163
}
191
164
}
192
165
193
166
#[ cfg( not( feature = "optimize_for_size" ) ) ]
194
- impl $unsigned {
167
+ impl $Unsigned {
195
168
#[ doc( hidden) ]
196
169
#[ unstable(
197
170
feature = "fmt_internals" ,
@@ -212,7 +185,7 @@ macro_rules! impl_Display {
212
185
let mut remain = self ;
213
186
214
187
// Format per four digits from the lookup table.
215
- // Four digits need a 16-bit $unsigned or wider.
188
+ // Four digits need a 16-bit $Unsigned or wider.
216
189
while size_of:: <Self >( ) > 1 && remain > 999 . try_into( ) . expect( "branch is not hit for types that cannot fit 999 (u8)" ) {
217
190
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
218
191
// and the while condition ensures at least 4 more decimals.
@@ -271,7 +244,7 @@ macro_rules! impl_Display {
271
244
}
272
245
}
273
246
274
- impl $signed {
247
+ impl $Signed {
275
248
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
276
249
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
277
250
///
@@ -281,15 +254,15 @@ macro_rules! impl_Display {
281
254
/// #![feature(int_format_into)]
282
255
/// use core::fmt::NumBuffer;
283
256
///
284
- #[ doc = concat!( "let n = 0" , stringify!( $signed ) , ";" ) ]
257
+ #[ doc = concat!( "let n = 0" , stringify!( $Signed ) , ";" ) ]
285
258
/// let mut buf = NumBuffer::new();
286
259
/// assert_eq!(n.format_into(&mut buf), "0");
287
260
///
288
- #[ doc = concat!( "let n1 = 32" , stringify!( $signed ) , ";" ) ]
261
+ #[ doc = concat!( "let n1 = 32" , stringify!( $Signed ) , ";" ) ]
289
262
/// assert_eq!(n1.format_into(&mut buf), "32");
290
263
///
291
- #[ doc = concat!( "let n2 = " , stringify!( $signed :: MAX ) , ";" ) ]
292
- #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $signed :: MAX ) , ".to_string());" ) ]
264
+ #[ doc = concat!( "let n2 = " , stringify!( $Signed :: MAX ) , ";" ) ]
265
+ #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $Signed :: MAX ) , ".to_string());" ) ]
293
266
/// ```
294
267
#[ unstable( feature = "int_format_into" , issue = "138215" ) ]
295
268
pub fn format_into( self , buf: & mut NumBuffer <Self >) -> & str {
@@ -302,7 +275,7 @@ macro_rules! impl_Display {
302
275
}
303
276
#[ cfg( feature = "optimize_for_size" ) ]
304
277
{
305
- offset = ${ concat( _inner_slow_integer_to_str, $gen_name ) } ( self . unsigned_abs( ) . $conv_fn ( ) , & mut buf. buf) ;
278
+ offset = ${ concat( _inner_slow_integer_to_str, $fmt_fn ) } ( self . unsigned_abs( ) as $T , & mut buf. buf) ;
306
279
}
307
280
// Only difference between signed and unsigned are these 4 lines.
308
281
if self < 0 {
@@ -314,7 +287,7 @@ macro_rules! impl_Display {
314
287
}
315
288
}
316
289
317
- impl $unsigned {
290
+ impl $Unsigned {
318
291
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
319
292
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
320
293
///
@@ -324,15 +297,15 @@ macro_rules! impl_Display {
324
297
/// #![feature(int_format_into)]
325
298
/// use core::fmt::NumBuffer;
326
299
///
327
- #[ doc = concat!( "let n = 0" , stringify!( $unsigned ) , ";" ) ]
300
+ #[ doc = concat!( "let n = 0" , stringify!( $Unsigned ) , ";" ) ]
328
301
/// let mut buf = NumBuffer::new();
329
302
/// assert_eq!(n.format_into(&mut buf), "0");
330
303
///
331
- #[ doc = concat!( "let n1 = 32" , stringify!( $unsigned ) , ";" ) ]
304
+ #[ doc = concat!( "let n1 = 32" , stringify!( $Unsigned ) , ";" ) ]
332
305
/// assert_eq!(n1.format_into(&mut buf), "32");
333
306
///
334
- #[ doc = concat!( "let n2 = " , stringify!( $unsigned :: MAX ) , ";" ) ]
335
- #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $unsigned :: MAX ) , ".to_string());" ) ]
307
+ #[ doc = concat!( "let n2 = " , stringify!( $Unsigned :: MAX ) , ";" ) ]
308
+ #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $Unsigned :: MAX ) , ".to_string());" ) ]
336
309
/// ```
337
310
#[ unstable( feature = "int_format_into" , issue = "138215" ) ]
338
311
pub fn format_into( self , buf: & mut NumBuffer <Self >) -> & str {
@@ -345,7 +318,7 @@ macro_rules! impl_Display {
345
318
}
346
319
#[ cfg( feature = "optimize_for_size" ) ]
347
320
{
348
- offset = ${ concat( _inner_slow_integer_to_str, $gen_name ) } ( self . $conv_fn ( ) , & mut buf. buf) ;
321
+ offset = ${ concat( _inner_slow_integer_to_str, $fmt_fn ) } ( * self as $T , & mut buf. buf) ;
349
322
}
350
323
// SAFETY: Starting from `offset`, all elements of the slice have been set.
351
324
unsafe { slice_buffer_to_str( & buf. buf, offset) }
@@ -356,7 +329,7 @@ macro_rules! impl_Display {
356
329
) *
357
330
358
331
#[ cfg( feature = "optimize_for_size" ) ]
359
- fn ${ concat( _inner_slow_integer_to_str, $gen_name ) } ( mut n: $u , buf: & mut [ MaybeUninit :: <u8 >] ) -> usize {
332
+ fn ${ concat( _inner_slow_integer_to_str, $fmt_fn ) } ( mut n: $T , buf: & mut [ MaybeUninit :: <u8 >] ) -> usize {
360
333
let mut curr = buf. len( ) ;
361
334
362
335
// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
@@ -377,11 +350,11 @@ macro_rules! impl_Display {
377
350
}
378
351
379
352
#[ cfg( feature = "optimize_for_size" ) ]
380
- fn $gen_name ( n: $u , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
381
- const MAX_DEC_N : usize = $u :: MAX . ilog( 10 ) as usize + 1 ;
353
+ fn $fmt_fn ( n: $T , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
354
+ const MAX_DEC_N : usize = $T :: MAX . ilog( 10 ) as usize + 1 ;
382
355
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
383
356
384
- let offset = ${ concat( _inner_slow_integer_to_str, $gen_name ) } ( n, & mut buf) ;
357
+ let offset = ${ concat( _inner_slow_integer_to_str, $fmt_fn ) } ( n, & mut buf) ;
385
358
// SAFETY: Starting from `offset`, all elements of the slice have been set.
386
359
let buf_slice = unsafe { slice_buffer_to_str( & buf, offset) } ;
387
360
f. pad_integral( is_nonnegative, "" , buf_slice)
@@ -390,9 +363,9 @@ macro_rules! impl_Display {
390
363
}
391
364
392
365
macro_rules! impl_Exp {
393
- ( $( $t : ident) , * as $u : ident via $conv_fn : ident named $name : ident) => {
394
- fn $name (
395
- mut n: $u ,
366
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
367
+ fn $fmt_fn (
368
+ mut n: $T ,
396
369
is_nonnegative: bool ,
397
370
upper: bool ,
398
371
f: & mut fmt:: Formatter <' _>
@@ -526,32 +499,41 @@ macro_rules! impl_Exp {
526
499
527
500
$(
528
501
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
529
- impl fmt:: LowerExp for $t {
530
- #[ allow( unused_comparisons) ]
502
+ impl fmt:: LowerExp for $Signed {
531
503
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
532
504
let is_nonnegative = * self >= 0 ;
533
505
let n = if is_nonnegative {
534
- self . $conv_fn ( )
506
+ * self as $T
535
507
} else {
536
- // convert the negative num to positive by summing 1 to its 2s complement
537
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
508
+ self . unsigned_abs( ) as $T
538
509
} ;
539
- $name( n, is_nonnegative, false , f)
510
+ $fmt_fn( n, is_nonnegative, false , f)
511
+ }
512
+ }
513
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
514
+ impl fmt:: LowerExp for $Unsigned {
515
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
516
+ $fmt_fn( * self as $T, true , false , f)
540
517
}
541
518
} ) *
519
+
542
520
$(
543
521
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
544
- impl fmt:: UpperExp for $t {
545
- #[ allow( unused_comparisons) ]
522
+ impl fmt:: UpperExp for $Signed {
546
523
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
547
524
let is_nonnegative = * self >= 0 ;
548
525
let n = if is_nonnegative {
549
- self . $conv_fn ( )
526
+ * self as $T
550
527
} else {
551
- // convert the negative num to positive by summing 1 to its 2s complement
552
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
528
+ self . unsigned_abs( ) as $T
553
529
} ;
554
- $name( n, is_nonnegative, true , f)
530
+ $fmt_fn( n, is_nonnegative, true , f)
531
+ }
532
+ }
533
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
534
+ impl fmt:: UpperExp for $Unsigned {
535
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
536
+ $fmt_fn( * self as $T, true , true , f)
555
537
}
556
538
} ) *
557
539
} ;
@@ -567,37 +549,20 @@ impl_Debug! {
567
549
#[ cfg( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ]
568
550
mod imp {
569
551
use super :: * ;
570
- impl_Display ! (
571
- i8 , u8 ,
572
- i16 , u16 ,
573
- i32 , u32 ,
574
- i64 , u64 ,
575
- isize , usize ,
576
- ; as u64 via to_u64 named fmt_u64
577
- ) ;
578
- impl_Exp ! (
579
- i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , usize , isize
580
- as u64 via to_u64 named exp_u64
581
- ) ;
552
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into fmt_u64) ;
553
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into exp_u64) ;
582
554
}
583
555
584
556
#[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
585
557
mod imp {
586
558
use super :: * ;
587
- impl_Display ! (
588
- i8 , u8 ,
589
- i16 , u16 ,
590
- i32 , u32 ,
591
- isize , usize ,
592
- ; as u32 via to_u32 named fmt_u32) ;
593
- impl_Display ! (
594
- i64 , u64 ,
595
- ; as u64 via to_u64 named fmt_u64) ;
596
-
597
- impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize as u32 via to_u32 named exp_u32) ;
598
- impl_Exp ! ( i64 , u64 as u64 via to_u64 named exp_u64) ;
559
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into fmt_u32) ;
560
+ impl_Display ! ( i64 , u64 ; as u64 into fmt_u64) ;
561
+
562
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into exp_u32) ;
563
+ impl_Exp ! ( i64 , u64 ; as u64 into exp_u64) ;
599
564
}
600
- impl_Exp ! ( i128 , u128 as u128 via to_u128 named exp_u128) ;
565
+ impl_Exp ! ( i128 , u128 ; as u128 into exp_u128) ;
601
566
602
567
const U128_MAX_DEC_N : usize = u128:: MAX . ilog10 ( ) as usize + 1 ;
603
568
0 commit comments