Skip to content

Commit 7554f9f

Browse files
Rollup merge of rust-lang#133247 - GuillaumeGomez:reduce-integer-display-impl, r=workingjubilee
Reduce integer `Display` implementation size I was thinking about rust-lang#128204 and how we could reduce the size of the code and just realized that we didn't need the `_fmt` method to be implemented on signed integers, which in turns allow to simplify greatly the macro call. r? `@workingjubilee`
2 parents 7396c94 + ab38c6d commit 7554f9f

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

library/core/src/fmt/num.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -199,35 +199,43 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\
199199
8081828384858687888990919293949596979899";
200200

201201
macro_rules! impl_Display {
202-
($($t:ident $(as $positive:ident)? named $name:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
202+
($($signed:ident, $unsigned:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
203203

204204
$(
205205
#[stable(feature = "rust1", since = "1.0.0")]
206-
impl fmt::Display for $t {
206+
impl fmt::Display for $unsigned {
207207
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208-
// If it's a signed integer.
209-
$(
210-
let is_nonnegative = *self >= 0;
208+
#[cfg(not(feature = "optimize_for_size"))]
209+
{
210+
self._fmt(true, f)
211+
}
212+
#[cfg(feature = "optimize_for_size")]
213+
{
214+
$gen_name(self.$conv_fn(), true, f)
215+
}
216+
}
217+
}
211218

219+
#[stable(feature = "rust1", since = "1.0.0")]
220+
impl fmt::Display for $signed {
221+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222+
if *self < 0 {
212223
#[cfg(not(feature = "optimize_for_size"))]
213224
{
214-
if !is_nonnegative {
215-
// convert the negative num to positive by summing 1 to its 2s complement
216-
return (!self as $positive).wrapping_add(1)._fmt(false, f);
217-
}
225+
// convert the negative num to positive by summing 1 to its 2s complement
226+
return (!self as $unsigned).wrapping_add(1)._fmt(false, f);
218227
}
219228
#[cfg(feature = "optimize_for_size")]
220229
{
221-
if !is_nonnegative {
222-
// convert the negative num to positive by summing 1 to its 2s complement
223-
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
224-
}
230+
// convert the negative num to positive by summing 1 to its 2s complement
231+
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
225232
}
226-
)?
233+
}
234+
227235
// If it's a positive integer.
228236
#[cfg(not(feature = "optimize_for_size"))]
229237
{
230-
self._fmt(true, f)
238+
(*self as $unsigned)._fmt(true, f)
231239
}
232240
#[cfg(feature = "optimize_for_size")]
233241
{
@@ -237,9 +245,9 @@ macro_rules! impl_Display {
237245
}
238246

239247
#[cfg(not(feature = "optimize_for_size"))]
240-
impl $t {
241-
fn _fmt(mut self: $t, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
242-
const SIZE: usize = $t::MAX.ilog(10) as usize + 1;
248+
impl $unsigned {
249+
fn _fmt(mut self, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250+
const SIZE: usize = $unsigned::MAX.ilog(10) as usize + 1;
243251
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
244252
let mut curr = SIZE;
245253
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
@@ -258,7 +266,7 @@ macro_rules! impl_Display {
258266
#[allow(unused_comparisons)]
259267
// This block will be removed for smaller types at compile time and in the worst
260268
// case, it will prevent to have the `10000` literal to overflow for `i8` and `u8`.
261-
if core::mem::size_of::<$t>() >= 2 {
269+
if core::mem::size_of::<$unsigned>() >= 2 {
262270
// eagerly decode 4 characters at a time
263271
while self >= 10000 {
264272
let rem = (self % 10000) as usize;
@@ -312,8 +320,8 @@ macro_rules! impl_Display {
312320

313321
#[cfg(feature = "optimize_for_size")]
314322
fn $gen_name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
315-
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
316-
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
323+
const SIZE: usize = $u::MAX.ilog(10) as usize + 1;
324+
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
317325
let mut curr = buf.len();
318326
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
319327

@@ -523,16 +531,11 @@ impl_Debug! {
523531
mod imp {
524532
use super::*;
525533
impl_Display!(
526-
i8 as u8 named fmt_i8,
527-
u8 named fmt_u8,
528-
i16 as u16 named fmt_i16,
529-
u16 named fmt_u16,
530-
i32 as u32 named fmt_i32,
531-
u32 named fmt_u32,
532-
i64 as u64 named fmt_i64,
533-
u64 named fmt_u64,
534-
isize as usize named fmt_isize,
535-
usize named fmt_usize,
534+
i8, u8,
535+
i16, u16,
536+
i32, u32,
537+
i64, u64,
538+
isize, usize,
536539
; as u64 via to_u64 named fmt_u64
537540
);
538541
impl_Exp!(
@@ -545,18 +548,13 @@ mod imp {
545548
mod imp {
546549
use super::*;
547550
impl_Display!(
548-
i8 as u8 named fmt_i8,
549-
u8 named fmt_u8,
550-
i16 as u16 named fmt_i16,
551-
u16 named fmt_u16,
552-
i32 as u32 named fmt_i32,
553-
u32 named fmt_u32,
554-
isize as usize named fmt_isize,
555-
usize named fmt_usize,
551+
i8, u8,
552+
i16, u16,
553+
i32, u32,
554+
isize, usize,
556555
; as u32 via to_u32 named fmt_u32);
557556
impl_Display!(
558-
i64 as u64 named fmt_i64,
559-
u64 named fmt_u64,
557+
i64, u64,
560558
; as u64 via to_u64 named fmt_u64);
561559

562560
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);

0 commit comments

Comments
 (0)