orbtk_utils/
number.rs

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
use std::ops::Neg;

/// Valid number types (64bit)
#[derive(Debug, PartialOrd, PartialEq, Copy, Clone)]
pub enum Number {
    Real(i64),
    Float(f64),
}

impl Default for Number {
    fn default() -> Self {
        Self::Real(0)
    }
}

impl Neg for Number {
    type Output = Number;

    fn neg(self) -> Self::Output {
        match self {
            Number::Float(n) => Number::Float(-n),
            Number::Real(n) => Number::Real(-n),
        }
    }
}

macro_rules! impl_float {
    ($t:ty) => {
        impl From<$t> for Number {
            fn from(n: $t) -> Number {
                Number::Float(n as f64)
            }
        }

        impl From<Number> for $t {
            fn from(n: Number) -> Self {
                match n {
                    Number::Real(n) => n as $t,
                    Number::Float(n) => n as $t,
                }
            }
        }
    };
}

macro_rules! impl_real {
    ($t:ty) => {
        impl From<$t> for Number {
            fn from(n: $t) -> Number {
                Number::Real(n as i64)
            }
        }

        impl From<Number> for $t {
            fn from(n: Number) -> $t {
                match n {
                    Number::Real(n) => n as $t,
                    Number::Float(n) => n as $t,
                }
            }
        }
    };
}

impl_float!(f32);
impl_float!(f64);

impl_real!(u8);
impl_real!(i8);
impl_real!(u16);
impl_real!(i16);
impl_real!(u32);
impl_real!(i32);
impl_real!(u64);
impl_real!(i64);