1
Fork 0

NonZero checked_abs.

This commit is contained in:
Iago-lito 2021-04-15 12:19:24 +02:00
parent a433b06347
commit 62f97d950f

View file

@ -460,6 +460,39 @@ macro_rules! nonzero_signed_operations {
// SAFETY: This cannot overflow to zero.
unsafe { $Ty::new_unchecked(self.get().abs()) }
}
/// Checked absolute value.
/// Returns [`None`] if
#[doc = concat!("`self == ", stringify!($Int), "::MIN`.")]
///
/// # Example
///
/// ```
/// #![feature(nonzero_ops)]
/// # #![feature(try_trait)]
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
///
/// # fn main() -> Result<(), std::option::NoneError> {
#[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
#[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
#[doc = concat!("let min = ", stringify!($Ty), "::new(",
stringify!($Int), "::MIN)?;")]
///
/// assert_eq!(Some(pos), neg.checked_abs());
/// assert_eq!(None, min.checked_abs());
/// # Ok(())
/// # }
/// ```
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[inline]
pub const fn checked_abs(self) -> Option<$Ty> {
if let Some(nz) = self.get().checked_abs() {
// SAFETY: absolute value of nonzero cannot yield zero values.
Some(unsafe { $Ty::new_unchecked(nz) })
} else {
None
}
}
}
)+
}