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 c424403

Browse files
fmt::DisplayInt abstraction obsolete with better macro
1 parent 36a3735 commit c424403

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:literal, $dig_tab:literal) => {
@@ -151,49 +124,49 @@ unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
151124
}
152125

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

156129
$(
157130
#[stable(feature = "rust1", since = "1.0.0")]
158-
impl fmt::Display for $unsigned {
131+
impl fmt::Display for $Unsigned {
159132
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160133
#[cfg(not(feature = "optimize_for_size"))]
161134
{
162-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
163-
// Buffer decimals for $unsigned with right alignment.
135+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
136+
// Buffer decimals for self with right alignment.
164137
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
165138

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

176149
#[stable(feature = "rust1", since = "1.0.0")]
177-
impl fmt::Display for $signed {
150+
impl fmt::Display for $Signed {
178151
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179152
#[cfg(not(feature = "optimize_for_size"))]
180153
{
181-
const MAX_DEC_N: usize = $unsigned::MAX.ilog10() as usize + 1;
182-
// Buffer decimals for $unsigned with right alignment.
154+
const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
155+
// Buffer decimals for self with right alignment.
183156
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
184157

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

195168
#[cfg(not(feature = "optimize_for_size"))]
196-
impl $unsigned {
169+
impl $Unsigned {
197170
#[doc(hidden)]
198171
#[unstable(
199172
feature = "fmt_internals",
@@ -214,7 +187,7 @@ macro_rules! impl_Display {
214187
let mut remain = self;
215188

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

276-
impl $signed {
249+
impl $Signed {
277250
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
278251
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
279252
///
@@ -283,15 +256,15 @@ macro_rules! impl_Display {
283256
/// #![feature(int_format_into)]
284257
/// use core::fmt::NumBuffer;
285258
///
286-
#[doc = concat!("let n = 0", stringify!($signed), ";")]
259+
#[doc = concat!("let n = 0", stringify!($Signed), ";")]
287260
/// let mut buf = NumBuffer::new();
288261
/// assert_eq!(n.format_into(&mut buf), "0");
289262
///
290-
#[doc = concat!("let n1 = 32", stringify!($signed), ";")]
263+
#[doc = concat!("let n1 = 32", stringify!($Signed), ";")]
291264
/// assert_eq!(n1.format_into(&mut buf), "32");
292265
///
293-
#[doc = concat!("let n2 = ", stringify!($signed::MAX), ";")]
294-
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($signed::MAX), ".to_string());")]
266+
#[doc = concat!("let n2 = ", stringify!($Signed::MAX), ";")]
267+
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Signed::MAX), ".to_string());")]
295268
/// ```
296269
#[unstable(feature = "int_format_into", issue = "138215")]
297270
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
@@ -304,7 +277,7 @@ macro_rules! impl_Display {
304277
}
305278
#[cfg(feature = "optimize_for_size")]
306279
{
307-
offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(self.unsigned_abs().$conv_fn(), &mut buf.buf);
280+
offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(self.unsigned_abs()as $T, &mut buf.buf);
308281
}
309282
// Only difference between signed and unsigned are these 4 lines.
310283
if self < 0 {
@@ -316,7 +289,7 @@ macro_rules! impl_Display {
316289
}
317290
}
318291

319-
impl $unsigned {
292+
impl $Unsigned {
320293
/// Allows users to write an integer (in signed decimal format) into a variable `buf` of
321294
/// type [`NumBuffer`] that is passed by the caller by mutable reference.
322295
///
@@ -326,15 +299,15 @@ macro_rules! impl_Display {
326299
/// #![feature(int_format_into)]
327300
/// use core::fmt::NumBuffer;
328301
///
329-
#[doc = concat!("let n = 0", stringify!($unsigned), ";")]
302+
#[doc = concat!("let n = 0", stringify!($Unsigned), ";")]
330303
/// let mut buf = NumBuffer::new();
331304
/// assert_eq!(n.format_into(&mut buf), "0");
332305
///
333-
#[doc = concat!("let n1 = 32", stringify!($unsigned), ";")]
306+
#[doc = concat!("let n1 = 32", stringify!($Unsigned), ";")]
334307
/// assert_eq!(n1.format_into(&mut buf), "32");
335308
///
336-
#[doc = concat!("let n2 = ", stringify!($unsigned::MAX), ";")]
337-
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($unsigned::MAX), ".to_string());")]
309+
#[doc = concat!("let n2 = ", stringify!($Unsigned::MAX), ";")]
310+
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Unsigned::MAX), ".to_string());")]
338311
/// ```
339312
#[unstable(feature = "int_format_into", issue = "138215")]
340313
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
@@ -347,7 +320,7 @@ macro_rules! impl_Display {
347320
}
348321
#[cfg(feature = "optimize_for_size")]
349322
{
350-
offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(self.$conv_fn(), &mut buf.buf);
323+
offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(*selfas $T, &mut buf.buf);
351324
}
352325
// SAFETY: Starting from `offset`, all elements of the slice have been set.
353326
unsafe { slice_buffer_to_str(&buf.buf, offset) }
@@ -358,7 +331,7 @@ macro_rules! impl_Display {
358331
)*
359332

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

364337
// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
@@ -379,11 +352,11 @@ macro_rules! impl_Display {
379352
}
380353

381354
#[cfg(feature = "optimize_for_size")]
382-
fn $gen_name(n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383-
const MAX_DEC_N: usize = $u::MAX.ilog(10) as usize + 1;
355+
fn $fmt_fn(n: $T, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356+
const MAX_DEC_N: usize = $T::MAX.ilog(10) as usize + 1;
384357
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
385358

386-
let offset = ${concat(_inner_slow_integer_to_str, $gen_name)}(n, &mut buf);
359+
let offset = ${concat(_inner_slow_integer_to_str, $fmt_fn)}(n, &mut buf);
387360
// SAFETY: Starting from `offset`, all elements of the slice have been set.
388361
let buf_slice = unsafe { slice_buffer_to_str(&buf, offset) };
389362
f.pad_integral(is_nonnegative, "", buf_slice)
@@ -392,9 +365,9 @@ macro_rules! impl_Display {
392365
}
393366

394367
macro_rules! impl_Exp {
395-
($($t:ident),*as $u:ident via $conv_fn:ident named $name:ident) => {
396-
fn $name(
397-
mut n: $u,
368+
($($Signed:ident, $Unsigned:ident),*;as $T:ident into $fmt_fn:ident) => {
369+
fn $fmt_fn(
370+
mut n: $T,
398371
is_nonnegative: bool,
399372
upper: bool,
400373
f: &mut fmt::Formatter<'_>
@@ -528,32 +501,41 @@ macro_rules! impl_Exp {
528501

529502
$(
530503
#[stable(feature = "integer_exp_format", since = "1.42.0")]
531-
impl fmt::LowerExp for $t {
532-
#[allow(unused_comparisons)]
504+
impl fmt::LowerExp for $Signed {
533505
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
534506
let is_nonnegative = *self >= 0;
535507
let n = if is_nonnegative {
536-
self.$conv_fn()
508+
*selfas $T
537509
} else {
538-
// convert the negative num to positive by summing 1 to its 2s complement
539-
(!self.$conv_fn()).wrapping_add(1)
510+
self.unsigned_abs() as $T
540511
};
541-
$name(n, is_nonnegative, false, f)
512+
$fmt_fn(n, is_nonnegative, false, f)
513+
}
514+
}
515+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
516+
impl fmt::LowerExp for $Unsigned {
517+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
518+
$fmt_fn(*self as $T, true, false, f)
542519
}
543520
})*
521+
544522
$(
545523
#[stable(feature = "integer_exp_format", since = "1.42.0")]
546-
impl fmt::UpperExp for $t {
547-
#[allow(unused_comparisons)]
524+
impl fmt::UpperExp for $Signed {
548525
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
549526
let is_nonnegative = *self >= 0;
550527
let n = if is_nonnegative {
551-
self.$conv_fn()
528+
*selfas $T
552529
} else {
553-
// convert the negative num to positive by summing 1 to its 2s complement
554-
(!self.$conv_fn()).wrapping_add(1)
530+
self.unsigned_abs() as $T
555531
};
556-
$name(n, is_nonnegative, true, f)
532+
$fmt_fn(n, is_nonnegative, true, f)
533+
}
534+
}
535+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
536+
impl fmt::UpperExp for $Unsigned {
537+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
538+
$fmt_fn(*self as $T, true, true, f)
557539
}
558540
})*
559541
};
@@ -569,37 +551,20 @@ impl_Debug! {
569551
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
570552
mod imp {
571553
use super::*;
572-
impl_Display!(
573-
i8, u8,
574-
i16, u16,
575-
i32, u32,
576-
i64, u64,
577-
isize, usize,
578-
; as u64 via to_u64 named fmt_u64
579-
);
580-
impl_Exp!(
581-
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
582-
as u64 via to_u64 named exp_u64
583-
);
554+
impl_Display!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into fmt_u64);
555+
impl_Exp!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into exp_u64);
584556
}
585557

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

604569
const U128_MAX_DEC_N: usize = u128::MAX.ilog10() as usize + 1;
605570

0 commit comments

Comments
(0)

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