@@ -493,6 +493,40 @@ macro_rules! nonzero_signed_operations {
493
493
None
494
494
}
495
495
}
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
+ }
496
530
}
497
531
) +
498
532
}
0 commit comments