rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// Bit operations.
pub trait BitOps: Sized {
    /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
    /// than or equal to the number of bits in `self`.
    fn checked_shl(self, rhs: u32) -> Option<Self>;

    /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
    /// larger than or equal to the number of bits in `self`.
    fn checked_shr(self, rhs: u32) -> Option<Self>;

    /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
    /// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
    ///
    /// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
    /// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
    /// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
    /// which may be what you want instead.
    fn wrapping_shl(self, rhs: u32) -> Self;

    /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
    /// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
    ///
    /// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
    /// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
    /// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
    /// which may be what you want instead.
    fn wrapping_shr(self, rhs: u32) -> Self;

    /// Shifts self left by `rhs` bits.
    ///
    /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
    /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
    /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
    fn overflowing_shl(self, rhs: u32) -> (Self, bool);

    /// Shifts self right by `rhs` bits.
    ///
    /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
    /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
    /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
    fn overflowing_shr(self, rhs: u32) -> (Self, bool);

    /// Shifts the bits to the left by a specified amount, `n`,
    /// wrapping the truncated bits to the end of the resulting integer.
    ///
    /// Please note this isn't the same operation as the `<<` shifting operator!
    fn rotate_left(self, n: u32) -> Self;

    /// Shifts the bits to the right by a specified amount, `n`,
    /// wrapping the truncated bits to the beginning of the resulting
    /// integer.
    ///
    /// Please note this isn't the same operation as the `>>` shifting operator!
    fn rotate_right(self, n: u32) -> Self;

    /// Reverses the byte order of the integer.
    fn swap_bytes(self) -> Self;

    /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
    /// second least-significant bit becomes second most-significant bit, etc.
    fn reverse_bits(self) -> Self;
}

/// Implements [`BitOps`].
macro_rules! impl_bit {
    ($($t:ty),*) => {
        $(impl BitOps for $t {
            #[inline] fn checked_shl(self, rhs: u32) -> Option<Self> { self.checked_shl(rhs) }
            #[inline] fn checked_shr(self, rhs: u32) -> Option<Self> { self.checked_shr(rhs) }
            #[inline] fn wrapping_shl(self, rhs: u32) -> Self { self.wrapping_shl(rhs) }
            #[inline] fn wrapping_shr(self, rhs: u32) -> Self { self.wrapping_shr(rhs) }
            #[inline] fn overflowing_shl(self, rhs: u32) -> (Self, bool) { self.overflowing_shl(rhs) }
            #[inline] fn overflowing_shr(self, rhs: u32) -> (Self, bool) { self.overflowing_shr(rhs) }
            #[inline] fn rotate_left(self, n: u32) -> Self { self.rotate_left(n) }
            #[inline] fn rotate_right(self, n: u32) -> Self { self.rotate_right(n) }
            #[inline] fn swap_bytes(self) -> Self { self.swap_bytes() }
            #[inline] fn reverse_bits(self) -> Self { self.reverse_bits() }
        })*
    };
}

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