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
use num::{FromPrimitive, NumCast, ToPrimitive, Zero};
use std::ops::*;

pub trait Type:
    NumCast
    + FromPrimitive
    + Zero
    + Clone
    + Copy
    + Send
    + Sync
    + PartialEq
    + PartialOrd
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Rem<Output = Self>
{
    fn min_f() -> f64;
    fn max_f() -> f64;

    fn min() -> Self {
        Self::from_float(Self::min_f())
    }

    fn max() -> Self {
        Self::from_float(Self::max_f())
    }

    fn is_float() -> bool {
        let x = Self::to_float(&Self::from_float(0.5));
        x > 0.0 && x < 1.0
    }

    fn is_signed() -> bool {
        Self::to_float(&Self::from_float(-1.0)) < 0.0
    }

    #[inline]
    fn normalize(f: f64) -> f64 {
        (f - Self::min_f()) / (Self::max_f() - Self::min_f())
    }

    #[inline]
    fn denormalize(f: f64) -> f64 {
        f * Self::max_f() - Self::min_f()
    }

    #[inline]
    fn to_float(x: &Self) -> f64 {
        match ToPrimitive::to_f64(x) {
            Some(f) => f,
            None => 0.0,
        }
    }

    #[inline]
    fn from_float(x: f64) -> Self {
        match FromPrimitive::from_f64(x) {
            Some(p) => p,
            None => Self::zero(),
        }
    }

    #[inline]
    fn clamp(f: f64) -> f64 {
        if f > Self::max_f() {
            Self::max_f()
        } else if f < Self::min_f() {
            Self::min_f()
        } else {
            f
        }
    }

    #[inline]
    fn convert<X: Type>(&self) -> X {
        X::from_float(X::denormalize(Self::normalize(Self::to_float(self))))
    }
}

macro_rules! make_type {
    ($t:ty, $min:expr, $max:expr) => {
        impl Type for $t {
            fn min_f() -> f64 {
                match ToPrimitive::to_f64(&$min) {
                    Some(f) => f,
                    None => panic!("Invalid type minimum"),
                }
            }
            fn max_f() -> f64 {
                match ToPrimitive::to_f64(&$max) {
                    Some(f) => f,
                    None => panic!("Invalid type maximum"),
                }
            }
        }
    };

    ($t:ty) => {
        make_type!($t, <$t>::min_value(), <$t>::max_value());
    };
}

make_type!(u8);
make_type!(u16);
make_type!(i32);
make_type!(u32);
make_type!(f32, 0, 1);
make_type!(i64);
make_type!(u64);
make_type!(f64, 0, 1);

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

    #[test]
    fn test_type_is_float() {
        assert_eq!(u8::is_float(), false);
        assert_eq!(u16::is_float(), false);
        assert_eq!(i32::is_float(), false);
        assert_eq!(u32::is_float(), false);
        assert_eq!(i64::is_float(), false);
        assert_eq!(u64::is_float(), false);
        assert_eq!(f32::is_float(), true);
        assert_eq!(f64::is_float(), true);
    }

    #[test]
    fn test_type_is_signed() {
        assert_eq!(u8::is_signed(), false);
        assert_eq!(u16::is_signed(), false);
        assert_eq!(i32::is_signed(), true);
        assert_eq!(u32::is_signed(), false);
        assert_eq!(i64::is_signed(), true);
        assert_eq!(u64::is_signed(), false);
        assert_eq!(f32::is_signed(), true);
        assert_eq!(f64::is_signed(), true);
    }
}