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