about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIago-lito <iago-lito@etak>2021-04-15 12:34:03 +0200
committerIago-lito <iago-lito@etak>2021-06-09 17:28:34 +0200
commitb8056d8e29b1ee82158bfa2a2fc65e09799ba1d6 (patch)
treea5ce599e4e75287fef567c2807d793d050b6dd53
parent7b37800b45e6cf5d06caf2ee21066797516ff43c (diff)
downloadrust-b8056d8e29b1ee82158bfa2a2fc65e09799ba1d6.tar.gz
rust-b8056d8e29b1ee82158bfa2a2fc65e09799ba1d6.zip
NonZero saturating_pow.
-rw-r--r--library/core/src/num/nonzero.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index ceb2e461387..90d87edb25d 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -770,6 +770,35 @@ macro_rules! nonzero_unsigned_signed_operations {
                         None
                     }
                 }
+
+                /// Raise non-zero value to an integer power.
+                #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
+                ///
+                /// # Examples
+                ///
+                /// ```
+                /// #![feature(nonzero_ops)]
+                /// # #![feature(try_trait)]
+                #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
+                ///
+                /// # fn main() -> Result<(), std::option::NoneError> {
+                #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")]
+                #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")]
+                #[doc = concat!("let max = ", stringify!($Ty), "::new(",
+                                stringify!($Int), "::MAX)?;")]
+                ///
+                /// assert_eq!(twenty_seven, three.saturating_pow(3));
+                /// assert_eq!(max, max.saturating_pow(3));
+                /// # Ok(())
+                /// # }
+                /// ```
+                #[unstable(feature = "nonzero_ops", issue = "84186")]
+                #[inline]
+                pub const fn saturating_pow(self, other: u32) -> $Ty {
+                    // SAFETY: saturating_pow returns u*::MAX on overflow
+                    // so the result cannot be zero.
+                    unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
+                }
             }
         )+
     }