Skip to content

Commit d593c6f

Browse files
committed
fmt::DisplayInt abstraction obsolete with better macro
1 parent 9e7a9b0 commit d593c6f

File tree

1 file changed

+53
-88
lines changed

1 file changed

+53
-88
lines changed

library/core/src/fmt/num.rs

Lines changed: 53 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,8 @@
33
use crate::fmt::NumBuffer;
44
use crate::mem::MaybeUninit;
55
use crate::num::fmt as numfmt;
6-
use crate::ops::{Div, Rem, Sub};
76
use 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.
369
macro_rules! radix_integer {
3710
(fmt::$Trait:ident for $Signed:ident and $Unsigned:ident, $prefix:expr, $dig_tab:expr) => {
@@ -157,49 +130,49 @@ unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
157130
}
158131

159132
macro_rules! impl_Display {
160-
($($signed:ident, $unsigned:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
133+
($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
161134

162135
$(
163136
#[stable(feature = "rust1", since = "1.0.0")]
164-
impl fmt::Display for $unsigned {
137+
impl fmt::Display for $Unsigned {
165138
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166139
#[cfg(not(feature = "optimize_for_size"))]
167140
{
168-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
169-
// Buffer decimals for $unsigned with right alignment.
141+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
142+
// Buffer decimals for self with right alignment.
170143
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
171144

172145
// SAFETY: `buf` is always big enough to contain all the digits.
173146
unsafe { f.pad_integral(true, "", self._fmt(&mut buf)) }
174147
}
175148
#[cfg(feature = "optimize_for_size")]
176149
{
177-
$gen_name(self.$conv_fn(), true, f)
150+
$fmt_fn(self as $T, true, f)
178151
}
179152
}
180153
}
181154

182155
#[stable(feature = "rust1", since = "1.0.0")]
183-
impl fmt::Display for $signed {
156+
impl fmt::Display for $Signed {
184157
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185158
#[cfg(not(feature = "optimize_for_size"))]
186159
{
187-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
188-
// Buffer decimals for $unsigned with right alignment.
160+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
161+
// Buffer decimals for self with right alignment.
189162
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
190163

191164
// SAFETY: `buf` is always big enough to contain all the digits.
192165
unsafe { f.pad_integral(*self >= 0, "", self.unsigned_abs()._fmt(&mut buf)) }
193166
}
194167
#[cfg(feature = "optimize_for_size")]
195168
{
196-
return $gen_name(self.unsigned_abs().$conv_fn(), *self >= 0, f);
169+
return $fmt_fn(self.unsigned_abs() as $T, *self >= 0, f);
197170
}
198171
}
199172
}
200173

201174
#[cfg(not(feature = "optimize_for_size"))]
202-
impl $unsigned {
175+
impl $Unsigned {
203176
#[doc(hidden)]
204177
#[unstable(
205178
feature = "fmt_internals",
@@ -279,7 +252,7 @@ macro_rules! impl_Display {
279252
}
280253
}
281254

282-
impl $signed {
255+
impl $Signed {
283256
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
284257
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
285258
///
@@ -310,7 +283,7 @@ macro_rules! impl_Display {
310283
}
311284
#[cfg(feature = "optimize_for_size")]
312285
{
313-
offset = _inner_slow_integer_to_str(self.unsigned_abs().$conv_fn(), &mut buf.buf);
286+
offset = _inner_slow_integer_to_str(self.unsigned_abs() as $T, &mut buf.buf);
314287
}
315288
// Only difference between signed and unsigned are these 4 lines.
316289
if self < 0 {
@@ -322,7 +295,7 @@ macro_rules! impl_Display {
322295
}
323296
}
324297

325-
impl $unsigned {
298+
impl $Unsigned {
326299
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
327300
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
328301
///
@@ -332,15 +305,15 @@ macro_rules! impl_Display {
332305
/// #![feature(int_format_into)]
333306
/// use core::fmt::NumBuffer;
334307
///
335-
#[doc = concat!("let n = 0", stringify!($unsigned), ";")]
308+
#[doc = concat!("let n = 0", stringify!($Unsigned), ";")]
336309
/// let mut buf = NumBuffer::new();
337310
/// assert_eq!(n.format_into(&mut buf), "0");
338311
///
339-
#[doc = concat!("let n1 = 32", stringify!($unsigned), ";")]
312+
#[doc = concat!("let n1 = 32", stringify!($Unsigned), ";")]
340313
/// assert_eq!(n1.format_into(&mut buf), "32");
341314
///
342-
#[doc = concat!("let n2 = ", stringify!($unsigned::MAX), ";")]
343-
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($unsigned::MAX), ".to_string());")]
315+
#[doc = concat!("let n2 = ", stringify!($Unsigned::MAX), ";")]
316+
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Unsigned::MAX), ".to_string());")]
344317
/// ```
345318
#[unstable(feature = "int_format_into", issue = "138215")]
346319
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
@@ -353,7 +326,7 @@ macro_rules! impl_Display {
353326
}
354327
#[cfg(feature = "optimize_for_size")]
355328
{
356-
offset = _inner_slow_integer_to_str(self.$conv_fn(), &mut buf.buf);
329+
offset = _inner_slow_integer_to_str(*self as $T, &mut buf.buf);
357330
}
358331
// SAFETY: Starting from `offset`, all elements of the slice have been set.
359332
unsafe { slice_buffer_to_str(&buf.buf, offset) }
@@ -364,7 +337,7 @@ macro_rules! impl_Display {
364337
)*
365338

366339
#[cfg(feature = "optimize_for_size")]
367-
fn _inner_slow_integer_to_str(mut n: $u, buf: &mut [MaybeUninit::<u8>]) -> usize {
340+
fn _inner_slow_integer_to_str(mut n: $T, buf: &mut [MaybeUninit::<u8>]) -> usize {
368341
let mut curr = buf.len();
369342

370343
// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
@@ -385,8 +358,8 @@ macro_rules! impl_Display {
385358
}
386359

387360
#[cfg(feature = "optimize_for_size")]
388-
fn $gen_name(n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
389-
const MAX_DEC_N: usize = $u::MAX.ilog(10) as usize + 1;
361+
fn $fmt_fn(n: $T, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
362+
const MAX_DEC_N: usize = $T::MAX.ilog(10) as usize + 1;
390363
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
391364

392365
let offset = _inner_slow_integer_to_str(n, &mut buf);
@@ -398,9 +371,9 @@ macro_rules! impl_Display {
398371
}
399372

400373
macro_rules! impl_Exp {
401-
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
402-
fn $name(
403-
mut n: $u,
374+
($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
375+
fn $fmt_fn(
376+
mut n: $T,
404377
is_nonnegative: bool,
405378
upper: bool,
406379
f: &mut fmt::Formatter<'_>
@@ -534,32 +507,41 @@ macro_rules! impl_Exp {
534507

535508
$(
536509
#[stable(feature = "integer_exp_format", since = "1.42.0")]
537-
impl fmt::LowerExp for $t {
538-
#[allow(unused_comparisons)]
510+
impl fmt::LowerExp for $Signed {
539511
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
540512
let is_nonnegative = *self >= 0;
541513
let n = if is_nonnegative {
542-
self.$conv_fn()
514+
*self as $T
543515
} else {
544-
// convert the negative num to positive by summing 1 to its 2s complement
545-
(!self.$conv_fn()).wrapping_add(1)
516+
self.unsigned_abs() as $T
546517
};
547-
$name(n, is_nonnegative, false, f)
518+
$fmt_fn(n, is_nonnegative, false, f)
519+
}
520+
}
521+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
522+
impl fmt::LowerExp for $Unsigned {
523+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
524+
$fmt_fn(*self as $T, true, false, f)
548525
}
549526
})*
527+
550528
$(
551529
#[stable(feature = "integer_exp_format", since = "1.42.0")]
552-
impl fmt::UpperExp for $t {
553-
#[allow(unused_comparisons)]
530+
impl fmt::UpperExp for $Signed {
554531
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
555532
let is_nonnegative = *self >= 0;
556533
let n = if is_nonnegative {
557-
self.$conv_fn()
534+
*self as $T
558535
} else {
559-
// convert the negative num to positive by summing 1 to its 2s complement
560-
(!self.$conv_fn()).wrapping_add(1)
536+
self.unsigned_abs() as $T
561537
};
562-
$name(n, is_nonnegative, true, f)
538+
$fmt_fn(n, is_nonnegative, true, f)
539+
}
540+
}
541+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
542+
impl fmt::UpperExp for $Unsigned {
543+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544+
$fmt_fn(*self as $T, true, true, f)
563545
}
564546
})*
565547
};
@@ -575,37 +557,20 @@ impl_Debug! {
575557
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
576558
mod imp {
577559
use super::*;
578-
impl_Display!(
579-
i8, u8,
580-
i16, u16,
581-
i32, u32,
582-
i64, u64,
583-
isize, usize,
584-
; as u64 via to_u64 named fmt_u64
585-
);
586-
impl_Exp!(
587-
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
588-
as u64 via to_u64 named exp_u64
589-
);
560+
impl_Display!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into fmt_u64);
561+
impl_Exp!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into exp_u64);
590562
}
591563

592564
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
593565
mod imp {
594566
use super::*;
595-
impl_Display!(
596-
i8, u8,
597-
i16, u16,
598-
i32, u32,
599-
isize, usize,
600-
; as u32 via to_u32 named fmt_u32);
601-
impl_Display!(
602-
i64, u64,
603-
; as u64 via to_u64 named fmt_u64);
604-
605-
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
606-
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
567+
impl_Display!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into fmt_u32);
568+
impl_Display!(i64, u64, ; as u64 into fmt_u64);
569+
570+
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into exp_u32);
571+
impl_Exp!(i64, u64; as u64 into exp_u64);
607572
}
608-
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
573+
impl_Exp!(i128, u128; as u128 into exp_u128);
609574

610575
const U128_MAX_DEC_N: usize = u128::MAX.ilog10() as usize + 1;
611576

0 commit comments

Comments
 (0)