@@ -561,6 +561,39 @@ macro_rules! nonzero_signed_operations {
561
561
// SAFETY: absolute value of nonzero cannot yield zero values.
562
562
unsafe { $Ty:: new_unchecked( self . get( ) . saturating_abs( ) ) }
563
563
}
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
+ }
564
597
}
565
598
) +
566
599
}
0 commit comments