33use crate :: fmt:: NumBuffer ;
44use crate :: mem:: MaybeUninit ;
55use crate :: num:: fmt as numfmt;
6- use crate :: ops:: { Div , Rem , Sub } ;
76use crate :: { fmt, ptr, slice, str} ;
87
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-
358// Formatting of integers with a non-decimal radix.
369macro_rules! radix_integer {
3710 ( 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 {
150123}
151124
152125macro_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) => {
154127
155128 $(
156129 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
157- impl fmt:: Display for $unsigned {
130+ impl fmt:: Display for $Unsigned {
158131 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
159132 #[ cfg( not( feature = "optimize_for_size" ) ) ]
160133 {
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.
163136 let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
164137
165138 // SAFETY: `buf` is always big enough to contain all the digits.
166139 unsafe { f. pad_integral( true , "" , self . _fmt( & mut buf) ) }
167140 }
168141 #[ cfg( feature = "optimize_for_size" ) ]
169142 {
170- $gen_name ( self . $conv_fn ( ) , true , f)
143+ $fmt_fn ( self as $T , true , f)
171144 }
172145 }
173146 }
174147
175148 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
176- impl fmt:: Display for $signed {
149+ impl fmt:: Display for $Signed {
177150 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
178151 #[ cfg( not( feature = "optimize_for_size" ) ) ]
179152 {
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.
182155 let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
183156
184157 // SAFETY: `buf` is always big enough to contain all the digits.
185158 unsafe { f. pad_integral( * self >= 0 , "" , self . unsigned_abs( ) . _fmt( & mut buf) ) }
186159 }
187160 #[ cfg( feature = "optimize_for_size" ) ]
188161 {
189- return $gen_name ( self . unsigned_abs( ) . $conv_fn ( ) , * self >= 0 , f) ;
162+ return $fmt_fn ( self . unsigned_abs( ) as $T , * self >= 0 , f) ;
190163 }
191164 }
192165 }
193166
194167 #[ cfg( not( feature = "optimize_for_size" ) ) ]
195- impl $unsigned {
168+ impl $Unsigned {
196169 #[ doc( hidden) ]
197170 #[ unstable(
198171 feature = "fmt_internals" ,
@@ -272,7 +245,7 @@ macro_rules! impl_Display {
272245 }
273246 }
274247
275- impl $signed {
248+ impl $Signed {
276249 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
277250 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
278251 ///
@@ -303,7 +276,7 @@ macro_rules! impl_Display {
303276 }
304277 #[ cfg( feature = "optimize_for_size" ) ]
305278 {
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) ;
307280 }
308281 // Only difference between signed and unsigned are these 4 lines.
309282 if self < 0 {
@@ -315,7 +288,7 @@ macro_rules! impl_Display {
315288 }
316289 }
317290
318- impl $unsigned {
291+ impl $Unsigned {
319292 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
320293 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
321294 ///
@@ -325,15 +298,15 @@ macro_rules! impl_Display {
325298 /// #![feature(int_format_into)]
326299 /// use core::fmt::NumBuffer;
327300 ///
328- #[ doc = concat!( "let n = 0" , stringify!( $unsigned ) , ";" ) ]
301+ #[ doc = concat!( "let n = 0" , stringify!( $Unsigned ) , ";" ) ]
329302 /// let mut buf = NumBuffer::new();
330303 /// assert_eq!(n.format_into(&mut buf), "0");
331304 ///
332- #[ doc = concat!( "let n1 = 32" , stringify!( $unsigned ) , ";" ) ]
305+ #[ doc = concat!( "let n1 = 32" , stringify!( $Unsigned ) , ";" ) ]
333306 /// assert_eq!(n1.format_into(&mut buf), "32");
334307 ///
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());" ) ]
337310 /// ```
338311 #[ unstable( feature = "int_format_into" , issue = "138215" ) ]
339312 pub fn format_into( self , buf: & mut NumBuffer <Self >) -> & str {
@@ -346,7 +319,7 @@ macro_rules! impl_Display {
346319 }
347320 #[ cfg( feature = "optimize_for_size" ) ]
348321 {
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) ;
350323 }
351324 // SAFETY: Starting from `offset`, all elements of the slice have been set.
352325 unsafe { slice_buffer_to_str( & buf. buf, offset) }
@@ -357,7 +330,7 @@ macro_rules! impl_Display {
357330 ) *
358331
359332 #[ 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 {
361334 let mut curr = buf. len( ) ;
362335
363336 // 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 {
378351 }
379352
380353 #[ 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 ;
383356 let mut buf = [ MaybeUninit :: <u8 >:: uninit( ) ; MAX_DEC_N ] ;
384357
385358 let offset = _inner_slow_integer_to_str( n, & mut buf) ;
@@ -391,9 +364,9 @@ macro_rules! impl_Display {
391364}
392365
393366macro_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 ,
397370 is_nonnegative: bool ,
398371 upper: bool ,
399372 f: & mut fmt:: Formatter <' _>
@@ -527,32 +500,41 @@ macro_rules! impl_Exp {
527500
528501 $(
529502 #[ 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 {
532504 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
533505 let is_nonnegative = * self >= 0 ;
534506 let n = if is_nonnegative {
535- self . $conv_fn ( )
507+ * self as $T
536508 } 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
539510 } ;
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)
541518 }
542519 } ) *
520+
543521 $(
544522 #[ 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 {
547524 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
548525 let is_nonnegative = * self >= 0 ;
549526 let n = if is_nonnegative {
550- self . $conv_fn ( )
527+ * self as $T
551528 } 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
554530 } ;
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)
556538 }
557539 } ) *
558540 } ;
@@ -568,37 +550,20 @@ impl_Debug! {
568550#[ cfg( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ]
569551mod imp {
570552 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) ;
583555}
584556
585557#[ cfg( not( any( target_pointer_width = "64" , target_arch = "wasm32" ) ) ) ]
586558mod imp {
587559 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) ;
600565}
601- impl_Exp ! ( i128 , u128 as u128 via to_u128 named exp_u128) ;
566+ impl_Exp ! ( i128 , u128 ; as u128 into exp_u128) ;
602567
603568const U128_MAX_DEC_N : usize = u128:: MAX . ilog10 ( ) as usize + 1 ;
604569
0 commit comments