Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e3b5ee

Browse files
fmt::DisplayInt abstraction obsolete with better macro
1 parent a96d8d6 commit 6e3b5ee

File tree

1 file changed

+59
-94
lines changed

1 file changed

+59
-94
lines changed

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

Lines changed: 59 additions & 94 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) => {
@@ -149,49 +122,49 @@ unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
149122
}
150123

151124
macro_rules! impl_Display {
152-
($($signed:ident, $unsigned:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
125+
($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
153126

154127
$(
155128
#[stable(feature = "rust1", since = "1.0.0")]
156-
impl fmt::Display for $unsigned {
129+
impl fmt::Display for $Unsigned {
157130
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158131
#[cfg(not(feature = "optimize_for_size"))]
159132
{
160-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
161-
// Buffer decimals for $unsigned with right alignment.
133+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
134+
// Buffer decimals for self with right alignment.
162135
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
163136

164137
// SAFETY: `buf` is always big enough to contain all the digits.
165138
unsafe { f.pad_integral(true, "", self._fmt(&mut buf)) }
166139
}
167140
#[cfg(feature = "optimize_for_size")]
168141
{
169-
$gen_name(self.$conv_fn(), true, f)
142+
$fmt_fn(selfas $T, true, f)
170143
}
171144
}
172145
}
173146

174147
#[stable(feature = "rust1", since = "1.0.0")]
175-
impl fmt::Display for $signed {
148+
impl fmt::Display for $Signed {
176149
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177150
#[cfg(not(feature = "optimize_for_size"))]
178151
{
179-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
180-
// Buffer decimals for $unsigned with right alignment.
152+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
153+
// Buffer decimals for self with right alignment.
181154
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
182155

183156
// SAFETY: `buf` is always big enough to contain all the digits.
184157
unsafe { f.pad_integral(*self >= 0, "", self.unsigned_abs()._fmt(&mut buf)) }
185158
}
186159
#[cfg(feature = "optimize_for_size")]
187160
{
188-
return $gen_name(self.unsigned_abs().$conv_fn(), *self >= 0, f);
161+
return $fmt_fn(self.unsigned_abs()as $T, *self >= 0, f);
189162
}
190163
}
191164
}
192165

193166
#[cfg(not(feature = "optimize_for_size"))]
194-
impl $unsigned {
167+
impl $Unsigned {
195168
#[doc(hidden)]
196169
#[unstable(
197170
feature = "fmt_internals",
@@ -212,7 +185,7 @@ macro_rules! impl_Display {
212185
let mut remain = self;
213186

214187
// Format per four digits from the lookup table.
215-
// Four digits need a 16-bit $unsigned or wider.
188+
// Four digits need a 16-bit $Unsigned or wider.
216189
while size_of::<Self>() > 1 && remain > 999.try_into().expect("branch is not hit for types that cannot fit 999 (u8)") {
217190
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
218191
// and the while condition ensures at least 4 more decimals.
@@ -271,7 +244,7 @@ macro_rules! impl_Display {
271244
}
272245
}
273246

274-
impl $signed {
247+
impl $Signed {
275248
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
276249
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
277250
///
@@ -281,15 +254,15 @@ macro_rules! impl_Display {
281254
/// #![feature(int_format_into)]
282255
/// use core::fmt::NumBuffer;
283256
///
284-
#[doc = concat!("let n = 0", stringify!($signed), ";")]
257+
#[doc = concat!("let n = 0", stringify!($Signed), ";")]
285258
/// let mut buf = NumBuffer::new();
286259
/// assert_eq!(n.format_into(&mut buf), "0");
287260
///
288-
#[doc = concat!("let n1 = 32", stringify!($signed), ";")]
261+
#[doc = concat!("let n1 = 32", stringify!($Signed), ";")]
289262
/// assert_eq!(n1.format_into(&mut buf), "32");
290263
///
291-
#[doc = concat!("let n2 = ", stringify!($signed::MAX), ";")]
292-
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($signed::MAX), ".to_string());")]
264+
#[doc = concat!("let n2 = ", stringify!($Signed::MAX), ";")]
265+
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Signed::MAX), ".to_string());")]
293266
/// ```
294267
#[unstable(feature = "int_format_into", issue = "138215")]
295268
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
@@ -302,7 +275,7 @@ macro_rules! impl_Display {
302275
}
303276
#[cfg(feature = "optimize_for_size")]
304277
{
305-
offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(self.unsigned_abs().$conv_fn(), &mut buf.buf);
278+
offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(self.unsigned_abs()as $T, &mut buf.buf);
306279
}
307280
// Only difference between signed and unsigned are these 4 lines.
308281
if self < 0 {
@@ -314,7 +287,7 @@ macro_rules! impl_Display {
314287
}
315288
}
316289

317-
impl $unsigned {
290+
impl $Unsigned {
318291
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
319292
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
320293
///
@@ -324,15 +297,15 @@ macro_rules! impl_Display {
324297
/// #![feature(int_format_into)]
325298
/// use core::fmt::NumBuffer;
326299
///
327-
#[doc = concat!("let n = 0", stringify!($unsigned), ";")]
300+
#[doc = concat!("let n = 0", stringify!($Unsigned), ";")]
328301
/// let mut buf = NumBuffer::new();
329302
/// assert_eq!(n.format_into(&mut buf), "0");
330303
///
331-
#[doc = concat!("let n1 = 32", stringify!($unsigned), ";")]
304+
#[doc = concat!("let n1 = 32", stringify!($Unsigned), ";")]
332305
/// assert_eq!(n1.format_into(&mut buf), "32");
333306
///
334-
#[doc = concat!("let n2 = ", stringify!($unsigned::MAX), ";")]
335-
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($unsigned::MAX), ".to_string());")]
307+
#[doc = concat!("let n2 = ", stringify!($Unsigned::MAX), ";")]
308+
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Unsigned::MAX), ".to_string());")]
336309
/// ```
337310
#[unstable(feature = "int_format_into", issue = "138215")]
338311
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
@@ -345,7 +318,7 @@ macro_rules! impl_Display {
345318
}
346319
#[cfg(feature = "optimize_for_size")]
347320
{
348-
offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(self.$conv_fn(), &mut buf.buf);
321+
offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(*selfas $T, &mut buf.buf);
349322
}
350323
// SAFETY: Starting from `offset`, all elements of the slice have been set.
351324
unsafe { slice_buffer_to_str(&buf.buf, offset) }
@@ -356,7 +329,7 @@ macro_rules! impl_Display {
356329
)*
357330

358331
#[cfg(feature = "optimize_for_size")]
359-
fn ${concat(_inner_slow_integer_to_str, $gen_name)}(mut n: $u, buf: &mut [MaybeUninit::<u8>]) -> usize {
332+
fn ${concat(_inner_slow_integer_to_str, $fmt_fn)}(mut n: $T, buf: &mut [MaybeUninit::<u8>]) -> usize {
360333
let mut curr = buf.len();
361334

362335
// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
@@ -377,11 +350,11 @@ macro_rules! impl_Display {
377350
}
378351

379352
#[cfg(feature = "optimize_for_size")]
380-
fn $gen_name(n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
381-
const MAX_DEC_N: usize = $u::MAX.ilog(10) as usize + 1;
353+
fn $fmt_fn(n: $T, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
354+
const MAX_DEC_N: usize = $T::MAX.ilog(10) as usize + 1;
382355
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
383356

384-
let offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(n, &mut buf);
357+
let offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(n, &mut buf);
385358
// SAFETY: Starting from `offset`, all elements of the slice have been set.
386359
let buf_slice = unsafe { slice_buffer_to_str(&buf, offset) };
387360
f.pad_integral(is_nonnegative, "", buf_slice)
@@ -390,9 +363,9 @@ macro_rules! impl_Display {
390363
}
391364

392365
macro_rules! impl_Exp {
393-
($($t:ident),*as $u:ident via $conv_fn:ident named $name:ident) => {
394-
fn $name(
395-
mut n: $u,
366+
($($Signed:ident, $Unsigned:ident),*;as $T:ident into $fmt_fn:ident) => {
367+
fn $fmt_fn(
368+
mut n: $T,
396369
is_nonnegative: bool,
397370
upper: bool,
398371
f: &mut fmt::Formatter<'_>
@@ -526,32 +499,41 @@ macro_rules! impl_Exp {
526499

527500
$(
528501
#[stable(feature = "integer_exp_format", since = "1.42.0")]
529-
impl fmt::LowerExp for $t {
530-
#[allow(unused_comparisons)]
502+
impl fmt::LowerExp for $Signed {
531503
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
532504
let is_nonnegative = *self >= 0;
533505
let n = if is_nonnegative {
534-
self.$conv_fn()
506+
*selfas $T
535507
} else {
536-
// convert the negative num to positive by summing 1 to its 2s complement
537-
(!self.$conv_fn()).wrapping_add(1)
508+
self.unsigned_abs() as $T
538509
};
539-
$name(n, is_nonnegative, false, f)
510+
$fmt_fn(n, is_nonnegative, false, f)
511+
}
512+
}
513+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
514+
impl fmt::LowerExp for $Unsigned {
515+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
516+
$fmt_fn(*self as $T, true, false, f)
540517
}
541518
})*
519+
542520
$(
543521
#[stable(feature = "integer_exp_format", since = "1.42.0")]
544-
impl fmt::UpperExp for $t {
545-
#[allow(unused_comparisons)]
522+
impl fmt::UpperExp for $Signed {
546523
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
547524
let is_nonnegative = *self >= 0;
548525
let n = if is_nonnegative {
549-
self.$conv_fn()
526+
*selfas $T
550527
} else {
551-
// convert the negative num to positive by summing 1 to its 2s complement
552-
(!self.$conv_fn()).wrapping_add(1)
528+
self.unsigned_abs() as $T
553529
};
554-
$name(n, is_nonnegative, true, f)
530+
$fmt_fn(n, is_nonnegative, true, f)
531+
}
532+
}
533+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
534+
impl fmt::UpperExp for $Unsigned {
535+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
536+
$fmt_fn(*self as $T, true, true, f)
555537
}
556538
})*
557539
};
@@ -567,37 +549,20 @@ impl_Debug! {
567549
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
568550
mod imp {
569551
use super::*;
570-
impl_Display!(
571-
i8, u8,
572-
i16, u16,
573-
i32, u32,
574-
i64, u64,
575-
isize, usize,
576-
; as u64 via to_u64 named fmt_u64
577-
);
578-
impl_Exp!(
579-
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
580-
as u64 via to_u64 named exp_u64
581-
);
552+
impl_Display!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into fmt_u64);
553+
impl_Exp!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into exp_u64);
582554
}
583555

584556
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
585557
mod imp {
586558
use super::*;
587-
impl_Display!(
588-
i8, u8,
589-
i16, u16,
590-
i32, u32,
591-
isize, usize,
592-
; as u32 via to_u32 named fmt_u32);
593-
impl_Display!(
594-
i64, u64,
595-
; as u64 via to_u64 named fmt_u64);
596-
597-
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
598-
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
559+
impl_Display!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into fmt_u32);
560+
impl_Display!(i64, u64; as u64 into fmt_u64);
561+
562+
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into exp_u32);
563+
impl_Exp!(i64, u64; as u64 into exp_u64);
599564
}
600-
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
565+
impl_Exp!(i128, u128; as u128 into exp_u128);
601566

602567
const U128_MAX_DEC_N: usize = u128::MAX.ilog10() as usize + 1;
603568

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /