Rollup merge of #131730 - zlfn:master, r=tgross35

Refactor some `core::fmt` macros

While looking at the macros in `core::fmt`, find that the macros are not well organized. So I created a patch to fix it.

[`core/src/fmt/num.rs`](https://github.com/rust-lang/rust/blob/master/library/core/src/fmt/num.rs)
*  `impl_int!` and `impl_uint!` macro are **completly** same. It would be better to combine for readability
* `impl_int!` has a problem that the indenting is not uniform. It has unified into 4 spaces
* `debug` macro in `num` renamed to `impl_Debug`, And it was moved to a position close to the `impl_Display`.

[`core/src/fmt/float.rs`](https://github.com/rust-lang/rust/blob/master/library/core/src/fmt/float.rs)
[`core/src/fmt/nofloat.rs`](https://github.com/rust-lang/rust/blob/master/library/core/src/fmt/nofloat.rs)
* `floating` macro now receive multiple idents at once. It makes the code cleaner.
* Modified the panic message more clearly in fallback function of `cfg(no_fp_fmt_parse)`
This commit is contained in:
Matthias Krüger 2024-10-16 20:15:54 +02:00 committed by GitHub
commit 82952da360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 76 deletions

View File

@ -196,7 +196,8 @@ fn float_to_general_debug<T>(fmt: &mut Formatter<'_>, num: &T) -> Result
} }
macro_rules! floating { macro_rules! floating {
($ty:ident) => { ($($ty:ident)*) => {
$(
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl Debug for $ty { impl Debug for $ty {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
@ -224,11 +225,11 @@ fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
float_to_exponential_common(fmt, self, true) float_to_exponential_common(fmt, self, true)
} }
} }
)*
}; };
} }
floating! { f32 } floating! { f32 f64 }
floating! { f64 }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl Debug for f16 { impl Debug for f16 {

View File

@ -1,18 +1,17 @@
use crate::fmt::{Debug, Formatter, Result}; use crate::fmt::{Debug, Formatter, Result};
macro_rules! floating { macro_rules! floating {
($ty:ident) => { ($($ty:ident)*) => {
$(
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl Debug for $ty { impl Debug for $ty {
#[inline] #[inline]
fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result { fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result {
panic!("floating point support is turned off"); panic!("floating point fmt support is turned off");
} }
} }
)*
}; };
} }
floating! { f16 } floating! { f16 f32 f64 f128 }
floating! { f32 }
floating! { f64 }
floating! { f128 }

View File

@ -31,22 +31,11 @@ fn to_u128(&self) -> u128 { *self as u128 }
})* })*
) )
} }
macro_rules! impl_uint {
($($t:ident)*) => (
$(impl DisplayInt for $t {
fn zero() -> Self { 0 }
fn from_u8(u: u8) -> Self { u as Self }
fn to_u8(&self) -> u8 { *self as u8 }
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
fn to_u32(&self) -> u32 { *self as u32 }
fn to_u64(&self) -> u64 { *self as u64 }
fn to_u128(&self) -> u128 { *self as u128 }
})*
)
}
impl_int! { i8 i16 i32 i64 i128 isize } impl_int! {
impl_uint! { u8 u16 u32 u64 u128 usize } i8 i16 i32 i64 i128 isize
u8 u16 u32 u64 u128 usize
}
/// A type that represents a specific radix /// A type that represents a specific radix
/// ///
@ -178,8 +167,10 @@ macro_rules! integer {
integer! { i32, u32 } integer! { i32, u32 }
integer! { i64, u64 } integer! { i64, u64 }
integer! { i128, u128 } integer! { i128, u128 }
macro_rules! debug {
($($T:ident)*) => {$( macro_rules! impl_Debug {
($($T:ident)*) => {
$(
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for $T { impl fmt::Debug for $T {
#[inline] #[inline]
@ -193,11 +184,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
} }
} }
} }
)*}; )*
} };
debug! {
i8 i16 i32 i64 i128 isize
u8 u16 u32 u64 u128 usize
} }
// 2 digit decimal look up table // 2 digit decimal look up table
@ -521,6 +509,11 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}; };
} }
impl_Debug! {
i8 i16 i32 i64 i128 isize
u8 u16 u32 u64 u128 usize
}
// Include wasm32 in here since it doesn't reflect the native pointer size, and // Include wasm32 in here since it doesn't reflect the native pointer size, and
// often cares strongly about getting a smaller code size. // often cares strongly about getting a smaller code size.
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))] #[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]