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

Commit 6083b0a

Browse files
committed
NonZero overflowing_abs.
1 parent 62f97d9 commit 6083b0a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

library/core/src/num/nonzero.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,40 @@ macro_rules! nonzero_signed_operations {
493493
None
494494
}
495495
}
496+
497+
/// Computes the absolute value of self,
498+
/// with overflow information, see
499+
#[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
500+
///
501+
/// # Example
502+
///
503+
/// ```
504+
/// #![feature(nonzero_ops)]
505+
/// # #![feature(try_trait)]
506+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
507+
///
508+
/// # fn main() -> Result<(), std::option::NoneError> {
509+
#[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
510+
#[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
511+
#[doc = concat!("let min = ", stringify!($Ty), "::new(",
512+
stringify!($Int), "::MIN)?;")]
513+
///
514+
/// assert_eq!((pos, false), pos.overflowing_abs());
515+
/// assert_eq!((pos, false), neg.overflowing_abs());
516+
/// assert_eq!((min, true), min.overflowing_abs());
517+
/// # Ok(())
518+
/// # }
519+
/// ```
520+
#[unstable(feature = "nonzero_ops", issue = "84186")]
521+
#[inline]
522+
pub const fn overflowing_abs(self) -> ($Ty, bool) {
523+
let (nz, flag) = self.get().overflowing_abs();
524+
(
525+
// SAFETY: absolute value of nonzero cannot yield zero values.
526+
unsafe { $Ty::new_unchecked(nz) },
527+
flag,
528+
)
529+
}
496530
}
497531
)+
498532
}

0 commit comments

Comments
 (0)