rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// 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);