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
/// Numeric types over which [`crate::Rect`] and [`crate::Vector2`] are defined
///
/// Types which implement this have basic operations and comparisons defined
/// that can work for all signed, unsigned numbers, integer, and floating point
/// numbers.
pub trait SpacialNumeric:
    std::cmp::PartialOrd
    + Copy
    + Clone
    + std::fmt::Debug
    + std::ops::Add<Self, Output = Self>
    + std::ops::Sub<Self, Output = Self>
    + std::ops::Mul<Self, Output = Self>
    + std::ops::Div<Self, Output = Self>
    + std::default::Default
{
}

/// Types which as well as being defined as SpacialNumeric, may be negated.
pub trait SignedSpacialNumeric: SpacialNumeric + std::ops::Neg<Output = Self> {}

/// Define the conversion between two SpacialNumeric types.
///
/// # Example
///
/// ```
/// # use riddle_math::*;
/// let a: u32 = 1;
/// let b: f32 = a.convert();
/// ```
pub trait SpacialNumericConversion<T> {
    /// Convert one SpacialNumeric value to another. This conversion can not fail.
    fn convert(self) -> T;
}

impl<T: SpacialNumeric> SpacialNumericConversion<T> for T {
    #[inline]
    fn convert(self) -> T {
        self
    }
}

macro_rules! define_spacial_numeric {
    (Conv, $f:ty, As($t:ty)) => {
        impl SpacialNumericConversion<$t> for $f {
            #[inline]
            fn convert(self) -> $t {
                self as $t
            }
        }
    };

    (Unsigned, $f:ty, ( $($ts:ident($t:ty)),* )) => {
        impl SpacialNumeric for $f {}

        $( define_spacial_numeric!(Conv, $f, $ts($t)); )*
    };
    (Signed, $f:ty, ( $($ts:ident($t:ty)),* )) => {
        impl SignedSpacialNumeric for $f {}

        define_spacial_numeric!(Unsigned, $f, ($( $ts($t) ),*));
    };
}

define_spacial_numeric!(Unsigned, u32, (As(i32), As(f32)));
define_spacial_numeric!(Signed, i32, (As(u32), As(f32)));
define_spacial_numeric!(Signed, f32, (As(u32), As(i32)));