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