Skip to content

Commit f8dc879

Browse files
committed
Add LowerExp and UpperExp implementations
Mark the new fmt impls with the correct rust version Clean up the fmt macro and format the tests
1 parent c65244c commit f8dc879

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

core/src/num/nonzero.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,40 @@ impl_zeroable_primitive!(
110110
pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
111111

112112
macro_rules! impl_nonzero_fmt {
113-
($Trait:ident) => {
114-
#[stable(feature = "nonzero", since = "1.28.0")]
115-
impl<T> fmt::$Trait for NonZero<T>
116-
where
117-
T: ZeroablePrimitive + fmt::$Trait,
118-
{
119-
#[inline]
120-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121-
self.get().fmt(f)
113+
($(#[$Attribute:meta] $Trait:ident)*) => {
114+
$(
115+
#[$Attribute]
116+
impl<T> fmt::$Trait for NonZero<T>
117+
where
118+
T: ZeroablePrimitive + fmt::$Trait,
119+
{
120+
#[inline]
121+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122+
self.get().fmt(f)
123+
}
122124
}
123-
}
125+
)*
124126
};
125127
}
126128

127-
impl_nonzero_fmt!(Debug);
128-
impl_nonzero_fmt!(Display);
129-
impl_nonzero_fmt!(Binary);
130-
impl_nonzero_fmt!(Octal);
131-
impl_nonzero_fmt!(LowerHex);
132-
impl_nonzero_fmt!(UpperHex);
129+
impl_nonzero_fmt! {
130+
#[stable(feature = "nonzero", since = "1.28.0")]
131+
Debug
132+
#[stable(feature = "nonzero", since = "1.28.0")]
133+
Display
134+
#[stable(feature = "nonzero", since = "1.28.0")]
135+
Binary
136+
#[stable(feature = "nonzero", since = "1.28.0")]
137+
Octal
138+
#[stable(feature = "nonzero", since = "1.28.0")]
139+
LowerHex
140+
#[stable(feature = "nonzero", since = "1.28.0")]
141+
UpperHex
142+
#[stable(feature = "nonzero_fmt_exp", since = "CURRENT_RUSTC_VERSION")]
143+
LowerExp
144+
#[stable(feature = "nonzero_fmt_exp", since = "CURRENT_RUSTC_VERSION")]
145+
UpperExp
146+
}
133147

134148
macro_rules! impl_nonzero_auto_trait {
135149
(unsafe $Trait:ident) => {

core/tests/nonzero.rs

+11
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,14 @@ fn test_signed_nonzero_neg() {
354354
assert_eq!((-NonZero::<i128>::new(1).unwrap()).get(), -1);
355355
assert_eq!((-NonZero::<i128>::new(-1).unwrap()).get(), 1);
356356
}
357+
358+
#[test]
359+
fn test_nonzero_fmt() {
360+
let i = format!("{0}, {0:?}, {0:x}, {0:X}, {0:#x}, {0:#X}, {0:o}, {0:b}, {0:e}, {0:E}", 42);
361+
let nz = format!(
362+
"{0}, {0:?}, {0:x}, {0:X}, {0:#x}, {0:#X}, {0:o}, {0:b}, {0:e}, {0:E}",
363+
NonZero::new(42).unwrap()
364+
);
365+
366+
assert_eq!(i, nz);
367+
}

0 commit comments

Comments
 (0)