Refactor floating macro and nofloat panic message

This commit is contained in:
zlfn 2024-10-15 18:39:30 +09:00
parent 0637517da6
commit 99af761632
3 changed files with 60 additions and 58 deletions

View File

@ -196,39 +196,40 @@ 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")] $(
impl Debug for $ty { #[stable(feature = "rust1", since = "1.0.0")]
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { impl Debug for $ty {
float_to_general_debug(fmt, self) fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
float_to_general_debug(fmt, self)
}
} }
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl Display for $ty { impl Display for $ty {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
float_to_decimal_display(fmt, self) float_to_decimal_display(fmt, self)
}
} }
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl LowerExp for $ty { impl LowerExp for $ty {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
float_to_exponential_common(fmt, self, false) float_to_exponential_common(fmt, self, false)
}
} }
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl UpperExp for $ty { impl UpperExp for $ty {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { 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")] $(
impl Debug for $ty { #[stable(feature = "rust1", since = "1.0.0")]
#[inline] impl Debug for $ty {
fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result { #[inline]
panic!("floating point support is turned off"); fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result {
panic!("floating point fmt support is turned off");
}
} }
} )*
}; };
} }
floating! { f16 } floating! { f16 f32 f64 f128 }
floating! { f32 }
floating! { f64 }
floating! { f128 }

View File

@ -20,15 +20,15 @@ trait DisplayInt:
macro_rules! impl_int { macro_rules! impl_int {
($($t:ident)*) => ( ($($t:ident)*) => (
$(impl DisplayInt for $t { $(impl DisplayInt for $t {
fn zero() -> Self { 0 } fn zero() -> Self { 0 }
fn from_u8(u: u8) -> Self { u as Self } fn from_u8(u: u8) -> Self { u as Self }
fn to_u8(&self) -> u8 { *self as u8 } fn to_u8(&self) -> u8 { *self as u8 }
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))] #[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
fn to_u32(&self) -> u32 { *self as u32 } fn to_u32(&self) -> u32 { *self as u32 }
fn to_u64(&self) -> u64 { *self as u64 } fn to_u64(&self) -> u64 { *self as u64 }
fn to_u128(&self) -> u128 { *self as u128 } fn to_u128(&self) -> u128 { *self as u128 }
})* })*
) )
} }
@ -169,21 +169,23 @@ macro_rules! integer {
integer! { i128, u128 } integer! { i128, u128 }
macro_rules! impl_Debug { macro_rules! impl_Debug {
($($T:ident)*) => {$( ($($T:ident)*) => {
#[stable(feature = "rust1", since = "1.0.0")] $(
impl fmt::Debug for $T { #[stable(feature = "rust1", since = "1.0.0")]
#[inline] impl fmt::Debug for $T {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { #[inline]
if f.debug_lower_hex() { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(self, f) if f.debug_lower_hex() {
} else if f.debug_upper_hex() { fmt::LowerHex::fmt(self, f)
fmt::UpperHex::fmt(self, f) } else if f.debug_upper_hex() {
} else { fmt::UpperHex::fmt(self, f)
fmt::Display::fmt(self, f) } else {
fmt::Display::fmt(self, f)
}
} }
} }
} )*
)*}; };
} }
// 2 digit decimal look up table // 2 digit decimal look up table
@ -508,8 +510,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
} }
impl_Debug! { impl_Debug! {
i8 i16 i32 i64 i128 isize i8 i16 i32 i64 i128 isize
u8 u16 u32 u64 u128 usize 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