Skip to content

Commit 5d8a673

Browse files
committed
NonZero saturating_pow.
1 parent 3041bd7 commit 5d8a673

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

core/src/num/nonzero.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,35 @@ macro_rules! nonzero_unsigned_signed_operations {
770770
None
771771
}
772772
}
773+
774+
/// Raise non-zero value to an integer power.
775+
#[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
776+
///
777+
/// # Examples
778+
///
779+
/// ```
780+
/// #![feature(nonzero_ops)]
781+
/// # #![feature(try_trait)]
782+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
783+
///
784+
/// # fn main() -> Result<(), std::option::NoneError> {
785+
#[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")]
786+
#[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")]
787+
#[doc = concat!("let max = ", stringify!($Ty), "::new(",
788+
stringify!($Int), "::MAX)?;")]
789+
///
790+
/// assert_eq!(twenty_seven, three.saturating_pow(3));
791+
/// assert_eq!(max, max.saturating_pow(3));
792+
/// # Ok(())
793+
/// # }
794+
/// ```
795+
#[unstable(feature = "nonzero_ops", issue = "84186")]
796+
#[inline]
797+
pub const fn saturating_pow(self, other: u32) -> $Ty {
798+
// SAFETY: saturating_pow returns u*::MAX on overflow
799+
// so the result cannot be zero.
800+
unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
801+
}
773802
}
774803
)+
775804
}

0 commit comments

Comments
 (0)