Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b6589bb

Browse files
committed
NonZero wrapping_abs.
1 parent 65e7321 commit b6589bb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

library/core/src/num/nonzero.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,39 @@ macro_rules! nonzero_signed_operations {
561561
// SAFETY: absolute value of nonzero cannot yield zero values.
562562
unsafe { $Ty::new_unchecked(self.get().saturating_abs()) }
563563
}
564+
565+
/// Wrapping absolute value, see
566+
#[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
567+
///
568+
/// # Example
569+
///
570+
/// ```
571+
/// #![feature(nonzero_ops)]
572+
/// # #![feature(try_trait)]
573+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
574+
///
575+
/// # fn main() -> Result<(), std::option::NoneError> {
576+
#[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
577+
#[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
578+
#[doc = concat!("let min = ", stringify!($Ty), "::new(",
579+
stringify!($Int), "::MIN)?;")]
580+
#[doc = concat!("let max = ", stringify!($Ty), "::new(",
581+
stringify!($Int), "::MAX)?;")]
582+
///
583+
/// assert_eq!(pos, pos.wrapping_abs());
584+
/// assert_eq!(pos, neg.wrapping_abs());
585+
/// assert_eq!(min, min.wrapping_abs());
586+
/// # // FIXME: add once Neg is implemented?
587+
/// # // assert_eq!(max, (-max).wrapping_abs());
588+
/// # Ok(())
589+
/// # }
590+
/// ```
591+
#[unstable(feature = "nonzero_ops", issue = "84186")]
592+
#[inline]
593+
pub const fn wrapping_abs(self) -> $Ty {
594+
// SAFETY: absolute value of nonzero cannot yield zero values.
595+
unsafe { $Ty::new_unchecked(self.get().wrapping_abs()) }
596+
}
564597
}
565598
)+
566599
}

0 commit comments

Comments
 (0)