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
use std::ops::*;
use super::real::Real;

macro_rules! impl_vec {
    ($type: ident, $count: tt, $($field: tt),*) => {
        #[derive(Copy, Clone, Debug, PartialEq)]
        #[derive(Serialize, Deserialize)]
        pub struct $type<R: Real = f32> {
            $(pub $field: R),*
        }

        impl<R: Real> $type<R> {
            pub fn new($($field: R),*) -> Self {
                Self { $($field),* }
            }
        }

        impl<R: Real> Add<Self> for $type<R> {
            type Output = Self;

            fn add(self, other: Self) -> Self::Output {
                $type::new($(self.$field + other.$field),*)
            }
        }

        impl<R: Real> AddAssign<Self> for $type<R> {
            fn add_assign(&mut self, other: Self) {
                $(self.$field += other.$field);*
            }
        }

        impl<R: Real> Sub<Self> for $type<R> {
            type Output = Self;

            fn sub(self, other: Self) -> Self::Output {
                $type::new($(self.$field - other.$field),*)
            }
        }
        
        impl<R: Real> SubAssign<Self> for $type<R> {
            fn sub_assign(&mut self, other: Self) {
                $(self.$field -= other.$field);*
            }
        }

        impl<R: Real> Mul<R> for $type<R> {
            type Output = Self;

            fn mul(self, other: R) -> Self::Output {
                $type::new($(self.$field * other),*)
            }
        }

        impl Mul<$type<f32>> for f32 {
            type Output = $type<f32>;

            fn mul(self, other: $type<f32>) -> Self::Output {
                $type::new($(self * other.$field),*)
            }
        }

        impl Mul<$type<f64>> for f64 {
            type Output = $type<f64>;

            fn mul(self, other: $type<f64>) -> Self::Output {
                $type::new($(self * other.$field),*)
            }
        }

        impl<R: Real> Into<[R; $count]> for $type<R> {
            fn into(self) -> [R; $count] {
                [$(self.$field),*]
            }
        }
    }
}

impl_vec!(Vec2, 2, x, y);
impl_vec!(Vec3, 3, x, y, z);
impl_vec!(Vec4, 4, x, y, z, w);

impl<R: Real> Vec2<R> {
    pub fn zero() -> Self {
        Self::new(R::zero(), R::zero())
    }
}

impl<R: Real> Vec3<R> {
    pub fn zero() -> Self {
        Self::new(R::zero(), R::zero(), R::zero())
    }
}

impl<R: Real> From<Vec2<R>> for Vec3<R> {
    fn from(vec2: Vec2<R>) -> Self {
        Self::new(vec2.x, vec2.y, R::zero())
    }
}