@@ -737,6 +737,39 @@ macro_rules! nonzero_unsigned_signed_operations {
737737 // SAFETY: The caller ensures there is no overflow.
738738 unsafe { $Ty:: new_unchecked( self . get( ) . unchecked_mul( other. get( ) ) ) }
739739 }
740+
741+ /// Raise non-zero value to an integer power.
742+ /// Return [`None`] on overflow.
743+ ///
744+ /// # Examples
745+ ///
746+ /// ```
747+ /// #![feature(nonzero_ops)]
748+ /// # #![feature(try_trait)]
749+ #[ doc = concat!( "# use std::num::" , stringify!( $Ty) , ";" ) ]
750+ ///
751+ /// # fn main() -> Result<(), std::option::NoneError> {
752+ #[ doc = concat!( "let three = " , stringify!( $Ty) , "::new(3)?;" ) ]
753+ #[ doc = concat!( "let twenty_seven = " , stringify!( $Ty) , "::new(27)?;" ) ]
754+ #[ doc = concat!( "let half_max = " , stringify!( $Ty) , "::new(" ,
755+ stringify!( $Int) , "::MAX / 2)?;" ) ]
756+ ///
757+ /// assert_eq!(Some(twenty_seven), three.checked_pow(3));
758+ /// assert_eq!(None, half_max.checked_pow(3));
759+ /// # Ok(())
760+ /// # }
761+ /// ```
762+ #[ unstable( feature = "nonzero_ops" , issue = "84186" ) ]
763+ #[ inline]
764+ pub const fn checked_pow( self , other: u32 ) -> Option <$Ty> {
765+ if let Some ( result) = self . get( ) . checked_pow( other) {
766+ // SAFETY: checked_pow returns None on overflow
767+ // so the result cannot be zero.
768+ Some ( unsafe { $Ty:: new_unchecked( result) } )
769+ } else {
770+ None
771+ }
772+ }
740773 }
741774 ) +
742775 }
0 commit comments