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) => {
@@ -157,49 +130,49 @@ unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
157
130
}
158
131
159
132
macro_rules! impl_Display {
160
- ( $( $signed : ident, $unsigned : ident, ) * ; as $u : ident via $conv_fn : ident named $gen_name : ident) => {
133
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
161
134
162
135
$(
163
136
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
164
- impl fmt:: Display for $unsigned {
137
+ impl fmt:: Display for $Unsigned {
165
138
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
166
139
#[ cfg( not( feature = "optimize_for_size" ) ) ]
167
140
{
168
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
169
- // Buffer decimals for $unsigned with right alignment.
141
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
142
+ // Buffer decimals for self with right alignment.
170
143
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
171
144
172
145
// SAFETY: `buf` is always big enough to contain all the digits.
173
146
unsafe { f. pad_integral( true , "" , self . _fmt( & mut buf) ) }
174
147
}
175
148
#[ cfg( feature = "optimize_for_size" ) ]
176
149
{
177
- $gen_name ( self . $conv_fn ( ) , true , f)
150
+ $fmt_fn ( self as $T , true , f)
178
151
}
179
152
}
180
153
}
181
154
182
155
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
183
- impl fmt:: Display for $signed {
156
+ impl fmt:: Display for $Signed {
184
157
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
185
158
#[ cfg( not( feature = "optimize_for_size" ) ) ]
186
159
{
187
- const MAX_DEC_N : usize = $unsigned :: MAX . ilog10( ) as usize + 1 ;
188
- // Buffer decimals for $unsigned with right alignment.
160
+ const MAX_DEC_N : usize = $Unsigned :: MAX . ilog10( ) as usize + 1 ;
161
+ // Buffer decimals for self with right alignment.
189
162
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
190
163
191
164
// SAFETY: `buf` is always big enough to contain all the digits.
192
165
unsafe { f. pad_integral( * self >= 0 , "" , self . unsigned_abs( ) . _fmt( & mut buf) ) }
193
166
}
194
167
#[ cfg( feature = "optimize_for_size" ) ]
195
168
{
196
- return $gen_name ( self . unsigned_abs( ) . $conv_fn ( ) , * self >= 0 , f) ;
169
+ return $fmt_fn ( self . unsigned_abs( ) as $T , * self >= 0 , f) ;
197
170
}
198
171
}
199
172
}
200
173
201
174
#[ cfg( not( feature = "optimize_for_size" ) ) ]
202
- impl $unsigned {
175
+ impl $Unsigned {
203
176
#[ doc( hidden) ]
204
177
#[ unstable(
205
178
feature = "fmt_internals" ,
@@ -279,7 +252,7 @@ macro_rules! impl_Display {
279
252
}
280
253
}
281
254
282
- impl $signed {
255
+ impl $Signed {
283
256
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
284
257
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
285
258
///
@@ -310,7 +283,7 @@ macro_rules! impl_Display {
310
283
}
311
284
#[ cfg( feature = "optimize_for_size" ) ]
312
285
{
313
- offset = _inner_slow_integer_to_str( self . unsigned_abs( ) . $conv_fn ( ) , & mut buf. buf) ;
286
+ offset = _inner_slow_integer_to_str( self . unsigned_abs( ) as $T , & mut buf. buf) ;
314
287
}
315
288
// Only difference between signed and unsigned are these 4 lines.
316
289
if self < 0 {
@@ -322,7 +295,7 @@ macro_rules! impl_Display {
322
295
}
323
296
}
324
297
325
- impl $unsigned {
298
+ impl $Unsigned {
326
299
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
327
300
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
328
301
///
@@ -332,15 +305,15 @@ macro_rules! impl_Display {
332
305
/// #![feature(int_format_into)]
333
306
/// use core::fmt::NumBuffer;
334
307
///
335
- #[ doc = concat!( "let n = 0" , stringify!( $unsigned ) , ";" ) ]
308
+ #[ doc = concat!( "let n = 0" , stringify!( $Unsigned ) , ";" ) ]
336
309
/// let mut buf = NumBuffer::new();
337
310
/// assert_eq!(n.format_into(&mut buf), "0");
338
311
///
339
- #[ doc = concat!( "let n1 = 32" , stringify!( $unsigned ) , ";" ) ]
312
+ #[ doc = concat!( "let n1 = 32" , stringify!( $Unsigned ) , ";" ) ]
340
313
/// assert_eq!(n1.format_into(&mut buf), "32");
341
314
///
342
- #[ doc = concat!( "let n2 = " , stringify!( $unsigned :: MAX ) , ";" ) ]
343
- #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $unsigned :: MAX ) , ".to_string());" ) ]
315
+ #[ doc = concat!( "let n2 = " , stringify!( $Unsigned :: MAX ) , ";" ) ]
316
+ #[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $Unsigned :: MAX ) , ".to_string());" ) ]
344
317
/// ```
345
318
#[ unstable( feature = "int_format_into" , issue = "138215" ) ]
346
319
pub fn format_into( self , buf: & mut NumBuffer <Self >) -> & str {
@@ -353,7 +326,7 @@ macro_rules! impl_Display {
353
326
}
354
327
#[ cfg( feature = "optimize_for_size" ) ]
355
328
{
356
- offset = _inner_slow_integer_to_str( self . $conv_fn ( ) , & mut buf. buf) ;
329
+ offset = _inner_slow_integer_to_str( * self as $T , & mut buf. buf) ;
357
330
}
358
331
// SAFETY: Starting from `offset`, all elements of the slice have been set.
359
332
unsafe { slice_buffer_to_str( & buf. buf, offset) }
@@ -364,7 +337,7 @@ macro_rules! impl_Display {
364
337
) *
365
338
366
339
#[ cfg( feature = "optimize_for_size" ) ]
367
- fn _inner_slow_integer_to_str( mut n: $u , buf: & mut [ MaybeUninit :: <u8 >] ) -> usize {
340
+ fn _inner_slow_integer_to_str( mut n: $T , buf: & mut [ MaybeUninit :: <u8 >] ) -> usize {
368
341
let mut curr = buf. len( ) ;
369
342
370
343
// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
@@ -385,8 +358,8 @@ macro_rules! impl_Display {
385
358
}
386
359
387
360
#[ cfg( feature = "optimize_for_size" ) ]
388
- fn $gen_name ( n: $u , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
389
- const MAX_DEC_N : usize = $u :: MAX . ilog( 10 ) as usize + 1 ;
361
+ fn $fmt_fn ( n: $T , is_nonnegative: bool , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
362
+ const MAX_DEC_N : usize = $T :: MAX . ilog( 10 ) as usize + 1 ;
390
363
let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
391
364
392
365
let offset = _inner_slow_integer_to_str( n, & mut buf) ;
@@ -398,9 +371,9 @@ macro_rules! impl_Display {
398
371
}
399
372
400
373
macro_rules! impl_Exp {
401
- ( $( $t : ident) , * as $u : ident via $conv_fn : ident named $name : ident) => {
402
- fn $name (
403
- mut n: $u ,
374
+ ( $( $Signed : ident, $Unsigned : ident) , * ; as $T : ident into $fmt_fn : ident) => {
375
+ fn $fmt_fn (
376
+ mut n: $T ,
404
377
is_nonnegative: bool ,
405
378
upper: bool ,
406
379
f: & mut fmt:: Formatter <' _>
@@ -534,32 +507,41 @@ macro_rules! impl_Exp {
534
507
535
508
$(
536
509
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
537
- impl fmt:: LowerExp for $t {
538
- #[ allow( unused_comparisons) ]
510
+ impl fmt:: LowerExp for $Signed {
539
511
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
540
512
let is_nonnegative = * self >= 0 ;
541
513
let n = if is_nonnegative {
542
- self . $conv_fn ( )
514
+ * self as $T
543
515
} else {
544
- // convert the negative num to positive by summing 1 to its 2s complement
545
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
516
+ self . unsigned_abs( ) as $T
546
517
} ;
547
- $name( n, is_nonnegative, false , f)
518
+ $fmt_fn( n, is_nonnegative, false , f)
519
+ }
520
+ }
521
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
522
+ impl fmt:: LowerExp for $Unsigned {
523
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
524
+ $fmt_fn( * self as $T, true , false , f)
548
525
}
549
526
} ) *
527
+
550
528
$(
551
529
#[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
552
- impl fmt:: UpperExp for $t {
553
- #[ allow( unused_comparisons) ]
530
+ impl fmt:: UpperExp for $Signed {
554
531
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
555
532
let is_nonnegative = * self >= 0 ;
556
533
let n = if is_nonnegative {
557
- self . $conv_fn ( )
534
+ * self as $T
558
535
} else {
559
- // convert the negative num to positive by summing 1 to its 2s complement
560
- ( !self . $conv_fn( ) ) . wrapping_add( 1 )
536
+ self . unsigned_abs( ) as $T
561
537
} ;
562
- $name( n, is_nonnegative, true , f)
538
+ $fmt_fn( n, is_nonnegative, true , f)
539
+ }
540
+ }
541
+ #[ stable( feature = "integer_exp_format" , since = "1.42.0" ) ]
542
+ impl fmt:: UpperExp for $Unsigned {
543
+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
544
+ $fmt_fn( * self as $T, true , true , f)
563
545
}
564
546
} ) *
565
547
} ;
@@ -575,37 +557,20 @@ impl_Debug! {
575
557
#[ cfg( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ]
576
558
mod imp {
577
559
use super :: * ;
578
- impl_Display ! (
579
- i8 , u8 ,
580
- i16 , u16 ,
581
- i32 , u32 ,
582
- i64 , u64 ,
583
- isize , usize ,
584
- ; as u64 via to_u64 named fmt_u64
585
- ) ;
586
- impl_Exp ! (
587
- i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , usize , isize
588
- as u64 via to_u64 named exp_u64
589
- ) ;
560
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into fmt_u64) ;
561
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , isize , usize ; as u64 into exp_u64) ;
590
562
}
591
563
592
564
#[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
593
565
mod imp {
594
566
use super :: * ;
595
- impl_Display ! (
596
- i8 , u8 ,
597
- i16 , u16 ,
598
- i32 , u32 ,
599
- isize , usize ,
600
- ; as u32 via to_u32 named fmt_u32) ;
601
- impl_Display ! (
602
- i64 , u64 ,
603
- ; as u64 via to_u64 named fmt_u64) ;
604
-
605
- impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize as u32 via to_u32 named exp_u32) ;
606
- impl_Exp ! ( i64 , u64 as u64 via to_u64 named exp_u64) ;
567
+ impl_Display ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into fmt_u32) ;
568
+ impl_Display ! ( i64 , u64 , ; as u64 into fmt_u64) ;
569
+
570
+ impl_Exp ! ( i8 , u8 , i16 , u16 , i32 , u32 , isize , usize ; as u32 into exp_u32) ;
571
+ impl_Exp ! ( i64 , u64 ; as u64 into exp_u64) ;
607
572
}
608
- impl_Exp ! ( i128 , u128 as u128 via to_u128 named exp_u128) ;
573
+ impl_Exp ! ( i128 , u128 ; as u128 into exp_u128) ;
609
574
610
575
const U128_MAX_DEC_N : usize = u128:: MAX . ilog10 ( ) as usize + 1 ;
611
576
0 commit comments