rutil 0.2.0

A library containing utilities for creating programs in rust.
Documentation
/// Clamp operations.
pub trait ClampOps {
    /// Compares and returns the minimum of two values.
    fn min(self, other: Self) -> Self;

    /// Compares and returns the maximum of two values.
    fn max(self, other: Self) -> Self;

    /// Restrict a value to a certain interval.
    ///
    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
    /// less than `min`. Otherwise this returns `self`.
    ///
    /// # Panics
    ///
    /// Panics if `min > max`.
    fn clamp(self, min: Self, max: Self) -> Self;
}

/// Implements [`ClampOps`] for integers.
macro_rules! impl_clamp_int {
    ($($t:ty),*) => {
        $(impl ClampOps for $t {
            #[inline] fn min(self, other: Self) -> Self { Ord::min(self, other) }
            #[inline] fn max(self, other: Self) -> Self { Ord::max(self, other) }
            #[inline] fn clamp(self, min: Self, max: Self) -> Self { Ord::clamp(self, min, max) }
        })*
    };
}

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

/// Implements [`ClampOps`] for floats.
macro_rules! impl_clamp_float {
    ($($t:ty),*) => {
        $(impl ClampOps for $t {
            #[inline] fn min(self, other: Self) -> Self { self.min(other) }
            #[inline] fn max(self, other: Self) -> Self { self.max(other) }
            #[inline] fn clamp(self, min: Self, max: Self) -> Self { self.clamp(min, max) }
        })*
    };
}

impl_clamp_float!(f32, f64);