Rollup merge of #131377 - rick-de-water:nonzero-exp, r=dtolnay
Add LowerExp and UpperExp implementations to NonZero Adds `LowerExp` and `UpperExp` trait implementations to `NonZero`, as discussed in rust-lang/libs-team#458. I had to modify the macro to mark the new impls with a different rust version. Let me know if this is the right way to do it (first timer here!)
This commit is contained in:
commit
b9f972767c
@ -110,26 +110,40 @@ unsafe impl ZeroablePrimitive for $primitive {
|
|||||||
pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
|
pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
|
||||||
|
|
||||||
macro_rules! impl_nonzero_fmt {
|
macro_rules! impl_nonzero_fmt {
|
||||||
($Trait:ident) => {
|
($(#[$Attribute:meta] $Trait:ident)*) => {
|
||||||
#[stable(feature = "nonzero", since = "1.28.0")]
|
$(
|
||||||
impl<T> fmt::$Trait for NonZero<T>
|
#[$Attribute]
|
||||||
where
|
impl<T> fmt::$Trait for NonZero<T>
|
||||||
T: ZeroablePrimitive + fmt::$Trait,
|
where
|
||||||
{
|
T: ZeroablePrimitive + fmt::$Trait,
|
||||||
#[inline]
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
#[inline]
|
||||||
self.get().fmt(f)
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
self.get().fmt(f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_nonzero_fmt!(Debug);
|
impl_nonzero_fmt! {
|
||||||
impl_nonzero_fmt!(Display);
|
#[stable(feature = "nonzero", since = "1.28.0")]
|
||||||
impl_nonzero_fmt!(Binary);
|
Debug
|
||||||
impl_nonzero_fmt!(Octal);
|
#[stable(feature = "nonzero", since = "1.28.0")]
|
||||||
impl_nonzero_fmt!(LowerHex);
|
Display
|
||||||
impl_nonzero_fmt!(UpperHex);
|
#[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 {
|
macro_rules! impl_nonzero_auto_trait {
|
||||||
(unsafe $Trait:ident) => {
|
(unsafe $Trait:ident) => {
|
||||||
|
@ -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);
|
||||||
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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user