Skip to content

Commit 2281143

Browse files
committed
fmt::DisplayInt abstraction obsolete with better macro
1 parent c8e628c commit 2281143

File tree

1 file changed

+44
-79
lines changed

1 file changed

+44
-79
lines changed

library/core/src/fmt/num.rs

Lines changed: 44 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,8 @@
22
33
use crate::mem::MaybeUninit;
44
use crate::num::fmt as numfmt;
5-
use crate::ops::{Div, Rem, Sub};
65
use crate::{fmt, ptr, slice, str};
76

8-
#[doc(hidden)]
9-
trait DisplayInt:
10-
PartialEq + PartialOrd + Div<Output = Self> + Rem<Output = Self> + Sub<Output = Self> + Copy
11-
{
12-
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
13-
fn to_u32(&self) -> u32;
14-
fn to_u64(&self) -> u64;
15-
fn to_u128(&self) -> u128;
16-
}
17-
18-
macro_rules! impl_int {
19-
($($t:ident)*) => (
20-
$(impl DisplayInt for $t {
21-
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
22-
fn to_u32(&self) -> u32 { *self as u32 }
23-
fn to_u64(&self) -> u64 { *self as u64 }
24-
fn to_u128(&self) -> u128 { *self as u128 }
25-
})*
26-
)
27-
}
28-
29-
impl_int! {
30-
i8 i16 i32 i64 i128 isize
31-
u8 u16 u32 u64 u128 usize
32-
}
33-
347
// Formatting of integers with a non-decimal radix.
358
macro_rules! radix_integer {
369
(fmt::$Trait:ident for $Signed:ident and $Unsigned:ident, $prefix:expr, $dig_tab:expr) => {
@@ -142,47 +115,47 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"\
142115
8081828384858687888990919293949596979899";
143116

144117
macro_rules! impl_Display {
145-
($($signed:ident, $unsigned:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
118+
($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
146119

147120
$(
148121
#[stable(feature = "rust1", since = "1.0.0")]
149-
impl fmt::Display for $unsigned {
122+
impl fmt::Display for $Unsigned {
150123
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151124
#[cfg(not(feature = "optimize_for_size"))]
152125
{
153-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
154-
// Buffer decimals for $unsigned with right alignment.
126+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
127+
// Buffer decimals for self with right alignment.
155128
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
156129

157130
f.pad_integral(true, "", self._fmt(&mut buf))
158131
}
159132
#[cfg(feature = "optimize_for_size")]
160133
{
161-
$gen_name(self.$conv_fn(), true, f)
134+
$fmt_fn(self as $T, true, f)
162135
}
163136
}
164137
}
165138

166139
#[stable(feature = "rust1", since = "1.0.0")]
167-
impl fmt::Display for $signed {
140+
impl fmt::Display for $Signed {
168141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169142
#[cfg(not(feature = "optimize_for_size"))]
170143
{
171-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
172-
// Buffer decimals for $unsigned with right alignment.
144+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
145+
// Buffer decimals for self with right alignment.
173146
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
174147

175148
f.pad_integral(*self >= 0, "", self.unsigned_abs()._fmt(&mut buf))
176149
}
177150
#[cfg(feature = "optimize_for_size")]
178151
{
179-
return $gen_name(self.unsigned_abs().$conv_fn(), *self >= 0, f);
152+
return $fmt_fn(self.unsigned_abs() as $T, *self >= 0, f);
180153
}
181154
}
182155
}
183156

184157
#[cfg(not(feature = "optimize_for_size"))]
185-
impl $unsigned {
158+
impl $Unsigned {
186159
#[doc(hidden)]
187160
#[unstable(
188161
feature = "fmt_internals",
@@ -264,8 +237,8 @@ macro_rules! impl_Display {
264237
})*
265238

266239
#[cfg(feature = "optimize_for_size")]
267-
fn $gen_name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
268-
const MAX_DEC_N: usize = $u::MAX.ilog10() as usize + 1;
240+
fn $fmt_fn(mut n: $T, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241+
const MAX_DEC_N: usize = $T::MAX.ilog10() as usize + 1;
269242
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
270243
let mut curr = MAX_DEC_N;
271244
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
@@ -298,9 +271,9 @@ macro_rules! impl_Display {
298271
}
299272

300273
macro_rules! impl_Exp {
301-
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
302-
fn $name(
303-
mut n: $u,
274+
($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
275+
fn $fmt_fn(
276+
mut n: $T,
304277
is_nonnegative: bool,
305278
upper: bool,
306279
f: &mut fmt::Formatter<'_>
@@ -434,32 +407,41 @@ macro_rules! impl_Exp {
434407

435408
$(
436409
#[stable(feature = "integer_exp_format", since = "1.42.0")]
437-
impl fmt::LowerExp for $t {
438-
#[allow(unused_comparisons)]
410+
impl fmt::LowerExp for $Signed {
439411
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
440412
let is_nonnegative = *self >= 0;
441413
let n = if is_nonnegative {
442-
self.$conv_fn()
414+
*self as $T
443415
} else {
444-
// convert the negative num to positive by summing 1 to its 2s complement
445-
(!self.$conv_fn()).wrapping_add(1)
416+
self.unsigned_abs() as $T
446417
};
447-
$name(n, is_nonnegative, false, f)
418+
$fmt_fn(n, is_nonnegative, false, f)
419+
}
420+
}
421+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
422+
impl fmt::LowerExp for $Unsigned {
423+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
424+
$fmt_fn(*self as $T, true, false, f)
448425
}
449426
})*
427+
450428
$(
451429
#[stable(feature = "integer_exp_format", since = "1.42.0")]
452-
impl fmt::UpperExp for $t {
453-
#[allow(unused_comparisons)]
430+
impl fmt::UpperExp for $Signed {
454431
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
455432
let is_nonnegative = *self >= 0;
456433
let n = if is_nonnegative {
457-
self.$conv_fn()
434+
*self as $T
458435
} else {
459-
// convert the negative num to positive by summing 1 to its 2s complement
460-
(!self.$conv_fn()).wrapping_add(1)
436+
self.unsigned_abs() as $T
461437
};
462-
$name(n, is_nonnegative, true, f)
438+
$fmt_fn(n, is_nonnegative, true, f)
439+
}
440+
}
441+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
442+
impl fmt::UpperExp for $Unsigned {
443+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444+
$fmt_fn(*self as $T, true, true, f)
463445
}
464446
})*
465447
};
@@ -475,37 +457,20 @@ impl_Debug! {
475457
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
476458
mod imp {
477459
use super::*;
478-
impl_Display!(
479-
i8, u8,
480-
i16, u16,
481-
i32, u32,
482-
i64, u64,
483-
isize, usize,
484-
; as u64 via to_u64 named fmt_u64
485-
);
486-
impl_Exp!(
487-
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
488-
as u64 via to_u64 named exp_u64
489-
);
460+
impl_Display!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into fmt_u64);
461+
impl_Exp!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into exp_u64);
490462
}
491463

492464
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
493465
mod imp {
494466
use super::*;
495-
impl_Display!(
496-
i8, u8,
497-
i16, u16,
498-
i32, u32,
499-
isize, usize,
500-
; as u32 via to_u32 named fmt_u32);
501-
impl_Display!(
502-
i64, u64,
503-
; as u64 via to_u64 named fmt_u64);
504-
505-
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
506-
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
467+
impl_Display!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into fmt_u32);
468+
impl_Display!(i64, u64, ; as u64 into fmt_u64);
469+
470+
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into exp_u32);
471+
impl_Exp!(i64, u64; as u64 into exp_u64);
507472
}
508-
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
473+
impl_Exp!(i128, u128; as u128 into exp_u128);
509474

510475
const U128_MAX_DEC_N: usize = u128::MAX.ilog10() as usize + 1;
511476

0 commit comments

Comments
 (0)