rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// Wrapping operations.
pub trait WrappingOps {
    /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
    /// boundary of the type.
    fn wrapping_add(self, rhs: Self) -> Self;

    /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
    /// boundary of the type.
    fn wrapping_sub(self, rhs: Self) -> Self;

    /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
    /// the boundary of the type.
    fn wrapping_mul(self, rhs: Self) -> Self;

    /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
    /// boundary of the type.
    ///
    /// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
    /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
    /// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn wrapping_div(self, rhs: Self) -> Self;

    /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
    /// boundary of the type.
    ///
    /// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
    /// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
    /// this function returns `0`.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn wrapping_rem(self, rhs: Self) -> Self;
}

/// Implements [`WrappingOps`].
macro_rules! impl_wrapping {
    ($($t:ty),*) => {
        $(impl WrappingOps for $t {
            #[inline] fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) }
            #[inline] fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) }
            #[inline] fn wrapping_mul(self, rhs: Self) -> Self { self.wrapping_mul(rhs) }
            #[inline] fn wrapping_div(self, rhs: Self) -> Self { self.wrapping_div(rhs) }
            #[inline] fn wrapping_rem(self, rhs: Self) -> Self { self.wrapping_rem(rhs) }
        })*
    };
}

impl_wrapping!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);