1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#![no_std]
//! Clamped floating-point types.
//!
//! Provides wrappers around floating-point values clamped to the range [0,1].
extern crate num_traits;

use core::fmt::{self, Debug, Display, Formatter, LowerExp, UpperExp};

#[cfg(feature = "std")]
use num_traits::float::Float;
#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore as Float;
use num_traits::Bounded;

/// A wrapper around a 32-bit floating-point value clamped to the range [0,1].
pub type Clamp32 = Clamp<f32>;
/// A wrapper around a 64-bit floating-point value clamped to the range [0,1].
pub type Clamp64 = Clamp<f64>;

/// A wrapper around a floating-point value clamped to the range [0,1].
#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub struct Clamp<T: Float>(T);

impl<T: Float> Clamp<T> {
    /// Creates a new wrapper around the value without checking.
    ///
    /// # Safety
    ///
    /// The value must be in the range [0,1].
    #[inline]
    pub unsafe fn new_unchecked(value: T) -> Self {
        Clamp(value)
    }

    /// Creates a new wrapper around the value.
    #[inline]
    pub fn new(value: T) -> Self {
        unsafe { Self::new_unchecked(clamp(value)) }
    }

    /// Sets the contained value without checking.
    ///
    /// # Safety
    ///
    /// The value must be in the range [0,1].
    #[inline]
    pub unsafe fn set_unchecked(&mut self, value: T) {
        self.0 = value;
    }

    /// Sets the contained value.
    #[inline]
    pub fn set(&mut self, value: T) {
        unsafe { self.set_unchecked(clamp(value)) }
    }

    /// Updates the contained value using a function without checking.
    ///
    /// # Safety
    ///
    /// The value produced by a function must be in the range [0,1].
    #[inline]
    pub unsafe fn update_unchecked<F>(&mut self, func: F)
    where
        F: FnOnce(T) -> T,
    {
        let current = self.get();
        let modified = func(current);

        self.set_unchecked(modified);
    }

    /// Updates the contained value using a function.
    #[inline]
    pub fn update<F>(&mut self, func: F)
    where
        F: FnOnce(T) -> T,
    {
        let current = self.get();
        let modified = func(current);

        self.set(modified);
    }

    /// Returns the contained value.
    #[inline]
    pub fn get(&self) -> T {
        self.0
    }
}

impl<T: Debug + Float> Debug for Clamp<T> {
    #[inline]
    fn fmt(&self, fmter: &mut Formatter) -> fmt::Result {
        self.get().fmt(fmter)
    }
}

impl<T: Display + Float> Display for Clamp<T> {
    #[inline]
    fn fmt(&self, fmter: &mut Formatter) -> fmt::Result {
        self.get().fmt(fmter)
    }
}

impl<T: LowerExp + Float> LowerExp for Clamp<T> {
    #[inline]
    fn fmt(&self, fmter: &mut Formatter) -> fmt::Result {
        self.get().fmt(fmter)
    }
}

impl<T: UpperExp + Float> UpperExp for Clamp<T> {
    #[inline]
    fn fmt(&self, fmter: &mut Formatter) -> fmt::Result {
        self.get().fmt(fmter)
    }
}

impl<T: Float> Default for Clamp<T> {
    #[inline]
    fn default() -> Self {
        Self::min_value()
    }
}

impl<T: Float> Bounded for Clamp<T> {
    #[inline]
    fn min_value() -> Self {
        unsafe { Self::new_unchecked(T::zero()) }
    }

    #[inline]
    fn max_value() -> Self {
        unsafe { Self::new_unchecked(T::one()) }
    }
}

/// Constrain the value to the range [0,1].
#[inline]
pub fn clamp<T: Float>(value: T) -> T {
    num_traits::clamp(value, T::zero(), T::one())
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! def_tests {
        ($($float:ident),+) => {
            $(
                mod $float {
                    use core::$float;
                    use super::*;

                    #[test]
                    fn test_clamp_wrapper() {
                        assert_eq!(Clamp::<$float>::new(0.0).get(), 0.0);
                        assert_eq!(Clamp::<$float>::new(0.5).get(), 0.5);
                        assert_eq!(Clamp::<$float>::new(1.0).get(), 1.0);

                        assert_eq!(Clamp::<$float>::new(-0.2).get(), 0.0);
                        assert_eq!(Clamp::<$float>::new(1.2).get(), 1.0);

                        assert_eq!(Clamp::new($float::NEG_INFINITY).get(), 0.0);
                        assert_eq!(Clamp::new($float::INFINITY).get(), 1.0);

                        assert_eq!(Clamp::new($float::MIN).get(), 0.0);
                        assert_eq!(Clamp::new($float::MAX).get(), 1.0);
                    }

                    #[test]
                    fn test_clamp_func() {
                        assert_eq!(clamp::<$float>(0.0), 0.0);
                        assert_eq!(clamp::<$float>(0.5), 0.5);
                        assert_eq!(clamp::<$float>(1.0), 1.0);

                        assert_eq!(clamp::<$float>(-0.2), 0.0);
                        assert_eq!(clamp::<$float>(1.2), 1.0);

                        assert_eq!(clamp($float::NEG_INFINITY), 0.0);
                        assert_eq!(clamp($float::INFINITY), 1.0);

                        assert_eq!(clamp($float::MIN), 0.0);
                        assert_eq!(clamp($float::MAX), 1.0);
                    }
                }
            )+
        };
    }

    def_tests!(f32, f64);
}