Skip to content

Commit 1562bf7

Browse files
Remove the need to provide the maximum number of digits to impl_Display macro
1 parent 884e0f0 commit 1562bf7

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

‎library/core/src/fmt/num.rs‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\
208208
8081828384858687888990919293949596979899";
209209

210210
macro_rules! impl_Display {
211-
($($t:ident => $size:literal $(as $positive:ident)? named $name:ident,)*;as $u:ident via $conv_fn:ident named $gen_name:ident) => {
211+
($($t:ident $(as $positive:ident)? named $name:ident,)*;as $u:ident via $conv_fn:ident named $gen_name:ident) => {
212212

213213
$(
214214
#[stable(feature = "rust1", since = "1.0.0")]
@@ -248,8 +248,9 @@ macro_rules! impl_Display {
248248
#[cfg(not(feature = "optimize_for_size"))]
249249
impl $t {
250250
fn _fmt(mutself: $t, is_nonnegative:bool, f:&mut fmt::Formatter<'_>) -> fmt::Result{
251-
letmut buf = [MaybeUninit::<u8>::uninit(); $size];
252-
letmut curr = $size;
251+
constSIZE:usize = $t::MAX.ilog(10)asusize + 1;
252+
letmut buf = [MaybeUninit::<u8>::uninit();SIZE];
253+
letmut curr = SIZE;
253254
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
254255
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
255256

@@ -526,16 +527,16 @@ macro_rules! impl_Exp {
526527
mod imp {
527528
usesuper::*;
528529
impl_Display!(
529-
i8=> 3asu8 named fmt_i8,
530-
u8=> 3named fmt_u8,
531-
i16=> 5asu16 named fmt_i16,
532-
u16=> 5named fmt_u16,
533-
i32=> 10asu32 named fmt_i32,
534-
u32=> 10named fmt_u32,
535-
i64=> 19asu64 named fmt_i64,
536-
u64=> 20named fmt_u64,
537-
isize=> 19asusize named fmt_isize,
538-
usize=> 20named fmt_usize,
530+
i8asu8 named fmt_i8,
531+
u8 named fmt_u8,
532+
i16asu16 named fmt_i16,
533+
u16 named fmt_u16,
534+
i32asu32 named fmt_i32,
535+
u32 named fmt_u32,
536+
i64asu64 named fmt_i64,
537+
u64 named fmt_u64,
538+
isizeasusize named fmt_isize,
539+
usize named fmt_usize,
539540
;asu64 via to_u64 named fmt_u64
540541
);
541542
impl_Exp!(
@@ -548,18 +549,18 @@ mod imp {
548549
mod imp {
549550
usesuper::*;
550551
impl_Display!(
551-
i8=> 3asu8 named fmt_i8,
552-
u8=> 3named fmt_u8,
553-
i16=> 5asu16 named fmt_i16,
554-
u16=> 5named fmt_u16,
555-
i32=> 10asu32 named fmt_i32,
556-
u32=> 10named fmt_u32,
557-
isize=> 19asusize named fmt_isize,
558-
usize=> 20named fmt_usize,
552+
i8asu8 named fmt_i8,
553+
u8 named fmt_u8,
554+
i16asu16 named fmt_i16,
555+
u16 named fmt_u16,
556+
i32asu32 named fmt_i32,
557+
u32 named fmt_u32,
558+
isizeasusize named fmt_isize,
559+
usize named fmt_usize,
559560
;asu32 via to_u32 named fmt_u32);
560561
impl_Display!(
561-
i64=> 19asu64 named fmt_i64,
562-
u64=> 20named fmt_u64,
562+
i64asu64 named fmt_i64,
563+
u64 named fmt_u64,
563564
;asu64 via to_u64 named fmt_u64);
564565

565566
impl_Exp!(i8,u8,i16,u16,i32,u32,isize,usizeasu32 via to_u32 named exp_u32);

0 commit comments

Comments
 (0)