Skip to content

Commit bfd32fa

Browse files
committed
Refactor floating macro and nofloat panic message
1 parent 148ed85 commit bfd32fa

File tree

3 files changed

+60
-58
lines changed

3 files changed

+60
-58
lines changed

core/src/fmt/float.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -196,39 +196,40 @@ where
196196
}
197197

198198
macro_rules! floating {
199-
($ty:ident) => {
200-
#[stable(feature = "rust1", since = "1.0.0")]
201-
impl Debug for $ty {
202-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
203-
float_to_general_debug(fmt, self)
199+
($($ty:ident)*) => {
200+
$(
201+
#[stable(feature = "rust1", since = "1.0.0")]
202+
impl Debug for $ty {
203+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
204+
float_to_general_debug(fmt, self)
205+
}
204206
}
205-
}
206207

207-
#[stable(feature = "rust1", since = "1.0.0")]
208-
impl Display for $ty {
209-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
210-
float_to_decimal_display(fmt, self)
208+
#[stable(feature = "rust1", since = "1.0.0")]
209+
impl Display for $ty {
210+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
211+
float_to_decimal_display(fmt, self)
212+
}
211213
}
212-
}
213214

214-
#[stable(feature = "rust1", since = "1.0.0")]
215-
impl LowerExp for $ty {
216-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
217-
float_to_exponential_common(fmt, self, false)
215+
#[stable(feature = "rust1", since = "1.0.0")]
216+
impl LowerExp for $ty {
217+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
218+
float_to_exponential_common(fmt, self, false)
219+
}
218220
}
219-
}
220221

221-
#[stable(feature = "rust1", since = "1.0.0")]
222-
impl UpperExp for $ty {
223-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
224-
float_to_exponential_common(fmt, self, true)
222+
#[stable(feature = "rust1", since = "1.0.0")]
223+
impl UpperExp for $ty {
224+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
225+
float_to_exponential_common(fmt, self, true)
226+
}
225227
}
226-
}
228+
)*
227229
};
228230
}
229231

230-
floating! { f32 }
231-
floating! { f64 }
232+
floating! { f32 f64 }
232233

233234
#[stable(feature = "rust1", since = "1.0.0")]
234235
impl Debug for f16 {

core/src/fmt/nofloat.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use crate::fmt::{Debug, Formatter, Result};
22

33
macro_rules! floating {
4-
($ty:ident) => {
5-
#[stable(feature = "rust1", since = "1.0.0")]
6-
impl Debug for $ty {
7-
#[inline]
8-
fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result {
9-
panic!("floating point support is turned off");
4+
($($ty:ident)*) => {
5+
$(
6+
#[stable(feature = "rust1", since = "1.0.0")]
7+
impl Debug for $ty {
8+
#[inline]
9+
fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result {
10+
panic!("floating point fmt support is turned off");
11+
}
1012
}
11-
}
13+
)*
1214
};
1315
}
1416

15-
floating! { f16 }
16-
floating! { f32 }
17-
floating! { f64 }
18-
floating! { f128 }
17+
floating! { f16 f32 f64 f128 }

core/src/fmt/num.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ trait DisplayInt:
2020

2121
macro_rules! impl_int {
2222
($($t:ident)*) => (
23-
$(impl DisplayInt for $t {
24-
fn zero() -> Self { 0 }
25-
fn from_u8(u: u8) -> Self { u as Self }
26-
fn to_u8(&self) -> u8 { *self as u8 }
27-
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
28-
fn to_u32(&self) -> u32 { *self as u32 }
29-
fn to_u64(&self) -> u64 { *self as u64 }
30-
fn to_u128(&self) -> u128 { *self as u128 }
31-
})*
23+
$(impl DisplayInt for $t {
24+
fn zero() -> Self { 0 }
25+
fn from_u8(u: u8) -> Self { u as Self }
26+
fn to_u8(&self) -> u8 { *self as u8 }
27+
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
28+
fn to_u32(&self) -> u32 { *self as u32 }
29+
fn to_u64(&self) -> u64 { *self as u64 }
30+
fn to_u128(&self) -> u128 { *self as u128 }
31+
})*
3232
)
3333
}
3434

@@ -169,21 +169,23 @@ integer! { i64, u64 }
169169
integer! { i128, u128 }
170170

171171
macro_rules! impl_Debug {
172-
($($T:ident)*) => {$(
173-
#[stable(feature = "rust1", since = "1.0.0")]
174-
impl fmt::Debug for $T {
175-
#[inline]
176-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177-
if f.debug_lower_hex() {
178-
fmt::LowerHex::fmt(self, f)
179-
} else if f.debug_upper_hex() {
180-
fmt::UpperHex::fmt(self, f)
181-
} else {
182-
fmt::Display::fmt(self, f)
172+
($($T:ident)*) => {
173+
$(
174+
#[stable(feature = "rust1", since = "1.0.0")]
175+
impl fmt::Debug for $T {
176+
#[inline]
177+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178+
if f.debug_lower_hex() {
179+
fmt::LowerHex::fmt(self, f)
180+
} else if f.debug_upper_hex() {
181+
fmt::UpperHex::fmt(self, f)
182+
} else {
183+
fmt::Display::fmt(self, f)
184+
}
183185
}
184186
}
185-
}
186-
)*};
187+
)*
188+
};
187189
}
188190

189191
// 2 digit decimal look up table
@@ -508,8 +510,8 @@ macro_rules! impl_Exp {
508510
}
509511

510512
impl_Debug! {
511-
i8 i16 i32 i64 i128 isize
512-
u8 u16 u32 u64 u128 usize
513+
i8 i16 i32 i64 i128 isize
514+
u8 u16 u32 u64 u128 usize
513515
}
514516

515517
// Include wasm32 in here since it doesn't reflect the native pointer size, and

0 commit comments

Comments
 (0)