Skip to content

Commit

Permalink
Add LowerExp and UpperExp implementations
Browse files Browse the repository at this point in the history
Mark the new fmt impls with the correct rust version

Clean up the fmt macro and format the tests
  • Loading branch information
rick-de-water committed Oct 8, 2024
1 parent c65244c commit f8dc879
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
46 changes: 30 additions & 16 deletions core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,40 @@ impl_zeroable_primitive!(
pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);

macro_rules! impl_nonzero_fmt {
($Trait:ident) => {
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::$Trait for NonZero<T>
where
T: ZeroablePrimitive + fmt::$Trait,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
($(#[$Attribute:meta] $Trait:ident)*) => {
$(
#[$Attribute]
impl<T> fmt::$Trait for NonZero<T>
where
T: ZeroablePrimitive + fmt::$Trait,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
}
)*
};
}

impl_nonzero_fmt!(Debug);
impl_nonzero_fmt!(Display);
impl_nonzero_fmt!(Binary);
impl_nonzero_fmt!(Octal);
impl_nonzero_fmt!(LowerHex);
impl_nonzero_fmt!(UpperHex);
impl_nonzero_fmt! {
#[stable(feature = "nonzero", since = "1.28.0")]
Debug
#[stable(feature = "nonzero", since = "1.28.0")]
Display
#[stable(feature = "nonzero", since = "1.28.0")]
Binary
#[stable(feature = "nonzero", since = "1.28.0")]
Octal
#[stable(feature = "nonzero", since = "1.28.0")]
LowerHex
#[stable(feature = "nonzero", since = "1.28.0")]
UpperHex
#[stable(feature = "nonzero_fmt_exp", since = "CURRENT_RUSTC_VERSION")]
LowerExp
#[stable(feature = "nonzero_fmt_exp", since = "CURRENT_RUSTC_VERSION")]
UpperExp
}

macro_rules! impl_nonzero_auto_trait {
(unsafe $Trait:ident) => {
Expand Down
11 changes: 11 additions & 0 deletions core/tests/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,14 @@ fn test_signed_nonzero_neg() {
assert_eq!((-NonZero::<i128>::new(1).unwrap()).get(), -1);
assert_eq!((-NonZero::<i128>::new(-1).unwrap()).get(), 1);
}

#[test]
fn test_nonzero_fmt() {
let i = format!("{0}, {0:?}, {0:x}, {0:X}, {0:#x}, {0:#X}, {0:o}, {0:b}, {0:e}, {0:E}", 42);
let nz = format!(
"{0}, {0:?}, {0:x}, {0:X}, {0:#x}, {0:#X}, {0:o}, {0:b}, {0:e}, {0:E}",
NonZero::new(42).unwrap()
);

assert_eq!(i, nz);
}

0 comments on commit f8dc879

Please sign in to comment.