rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// Sign operations for integers.
pub trait IntSignOps: Sized {
    /// Checked absolute value. Computes `self.abs()`, returning `None` if an overflow occurred.
    fn checked_abs(self) -> Option<Self>;

    /// Checked negation. Computes `-self`, returning `None` if an overflow occurred.
    fn checked_neg(self) -> Option<Self>;

    /// Saturating absolute value. Computes `self.abs()`, saturating at the numeric bounds instead of overflowing.
    fn saturating_abs(self) -> Self;

    /// Saturating negation. Computes `-self`, saturating at the numeric bounds instead of overflowing.
    fn saturating_neg(self) -> Self;

    /// Wrapping absolute value. Computes `self.abs()`, wrapping around at the boundary of the type.
    fn wrapping_abs(self) -> Self;

    /// Wrapping negation. Computes `-self`, wrapping around at the boundary of the type.
    fn wrapping_neg(self) -> Self;

    /// Computes the absolute value of `self`.
    ///
    /// Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow happened.
    fn overflowing_abs(self) -> (Self, bool);

    /// Computes the negation of `self`.
    /// 
    /// Returns a tuple of the negated version of self along with a boolean indicating whether an overflow happened.
    fn overflowing_neg(self) -> (Self, bool);
}

/// Implements [`IntSignOps`] for unsigned integers.
macro_rules! impl_int_sign_uint {
    ($($t:ty),*) => {
        $(impl IntSignOps for $t {
            #[inline] fn checked_abs(self) -> Option<Self> { Some(self) }
            #[inline] fn checked_neg(self) -> Option<Self> { self.checked_neg() }
            #[inline] fn saturating_abs(self) -> Self { self }
            #[inline] fn saturating_neg(self) -> Self { 0 }
            #[inline] fn wrapping_abs(self) -> Self { self }
            #[inline] fn wrapping_neg(self) -> Self { self.wrapping_neg() }
            #[inline] fn overflowing_abs(self) -> (Self, bool) { (self, false) }
            #[inline] fn overflowing_neg(self) -> (Self, bool) { self.overflowing_neg() }
        })*
    };
}

impl_int_sign_uint!(u8, u16, u32, u64, u128, usize);

/// Implements [`IntSignOps`] for signed integers.
macro_rules! impl_int_sign_int {
    ($($t:ty),*) => {
        $(impl IntSignOps for $t {
            #[inline] fn checked_abs(self) -> Option<Self> { self.checked_abs() }
            #[inline] fn checked_neg(self) -> Option<Self> { self.checked_neg() }
            #[inline] fn saturating_abs(self) -> Self { self.saturating_abs() }
            #[inline] fn saturating_neg(self) -> Self { self.saturating_neg() }
            #[inline] fn wrapping_abs(self) -> Self { self.wrapping_abs() }
            #[inline] fn wrapping_neg(self) -> Self { self.wrapping_neg() }
            #[inline] fn overflowing_abs(self) -> (Self, bool) { self.overflowing_abs() }
            #[inline] fn overflowing_neg(self) -> (Self, bool) { self.overflowing_neg() }
        })*
    };
}

impl_int_sign_int!(i8, i16, i32, i64, i128, isize);