1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// Power operations.
pub trait PowOps: Sized {
/// Raises self to the power of `exp`, using exponentiation by squaring.
fn pow(self, exp: u32) -> Self;
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
/// overflow occurred.
fn checked_pow(self, exp: u32) -> Option<Self>;
/// Saturating integer exponentiation. Computes `self.pow(exp)`,
/// saturating at the numeric bounds instead of overflowing.
fn saturating_pow(self, exp: u32) -> Self;
/// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
/// wrapping around at the boundary of the type.
fn wrapping_pow(self, exp: u32) -> Self;
/// Raises self to the power of `exp`, using exponentiation by squaring.
///
/// Returns a tuple of the exponentiation along with a bool indicating
/// whether an overflow happened.
fn overflowing_pow(self, exp: u32) -> (Self, bool);
}
/// Implements [`PowOps`].
macro_rules! impl_pow {
($($t:ty),*) => {
$(impl PowOps for $t {
#[inline] fn pow(self, exp: u32) -> Self { self.pow(exp) }
#[inline] fn checked_pow(self, exp: u32) -> Option<Self> { self.checked_pow(exp) }
#[inline] fn saturating_pow(self, exp: u32) -> Self { self.saturating_pow(exp) }
#[inline] fn wrapping_pow(self, exp: u32) -> Self { self.wrapping_pow(exp) }
#[inline] fn overflowing_pow(self, exp: u32) -> (Self, bool) { self.overflowing_pow(exp) }
})*
};
}
impl_pow!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);