rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// Euclid operations on integers.
pub trait IntEuclidOps: Sized {
    /// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
    /// returning `None` if `rhs == 0` or the division results in overflow.
    fn checked_div_euclid(self, rhs: Self) -> Option<Self>;

    /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
    /// if `rhs == 0` or the division results in overflow.
    fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;

    /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
    /// wrapping around at the boundary of the type.
    ///
    /// Wrapping will only occur in `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 this case, this method returns `MIN` itself.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn wrapping_div_euclid(self, rhs: Self) -> Self;

    /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
    /// at the boundary of the type.
    ///
    /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value
    /// for the type). In this case, this method returns 0.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn wrapping_rem_euclid(self, rhs: Self) -> Self;

    /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
    ///
    /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
    /// occur. If an overflow would occur then `self` is returned.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);

    /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
    ///
    /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
    /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
    ///
    /// # Panics
    ///
    /// This function will panic if `rhs` is 0.
    fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
}

/// Implements [`IntEuclidOps`].
macro_rules! impl_int_euclid {
    ($($t:ty),*) => {
        $(impl IntEuclidOps for $t {
            #[inline] fn checked_div_euclid(self, rhs: Self) -> Option<Self> { self.checked_div(rhs) }
            #[inline] fn checked_rem_euclid(self, rhs: Self) -> Option<Self> { self.checked_rem(rhs) }
            #[inline] fn wrapping_div_euclid(self, rhs: Self) -> Self { self.wrapping_div(rhs) }
            #[inline] fn wrapping_rem_euclid(self, rhs: Self) -> Self { self.wrapping_rem(rhs) }
            #[inline] fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) { self.overflowing_div(rhs) }
            #[inline] fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) { self.overflowing_rem(rhs) }
        })*
    };
}

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