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
use core::ops::{Add, Sub, Mul, Div};

pub use super::VecItem;

#[derive(Copy, Clone, Debug)]
pub struct Vec3<T: VecItem> {
    pub x: T,
    pub y: T,
    pub z: T,
}

impl<T: VecItem> Vec3<T> {
    pub fn new(x: T, y: T, z: T) -> Self { Self { x, y, z } }
}

impl<T: VecItem> From<[T; 3]> for Vec3<T> {
    fn from(arr: [T; 3]) -> Self { Self { x: arr[0], y: arr[1], z: arr[2] } }
}

impl<T: VecItem> From<(T, T, T)> for Vec3<T> {
    fn from(tup: (T, T, T)) -> Self { Self { x: tup.0, y: tup.1, z: tup.2 } }
}

impl<T> Add for Vec3<T> where T: VecItem + Add, T::Output: VecItem + Add + Copy {
    type Output = Vec3<T::Output>;
    fn add(self, other: Self) -> Vec3<T::Output> {
        Vec3 {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

impl<T> Sub for Vec3<T> where T: VecItem + Sub, T::Output: VecItem + Sub + Copy {
    type Output = Vec3<T::Output>;
    fn sub(self, other: Self) -> Vec3<T::Output> {
        Vec3 {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl<T> Mul for Vec3<T> where T: VecItem + Mul, T::Output: VecItem + Mul + Copy {
    type Output = Vec3<T::Output>;
    fn mul(self, other: Self) -> Vec3<T::Output> {
        Vec3 {
            x: self.x * other.x,
            y: self.y * other.y,
            z: self.z * other.z,
        }
    }
}

impl<T> Div for Vec3<T> where T: VecItem + Div, T::Output: VecItem + Div + Copy {
    type Output = Vec3<T::Output>;
    fn div(self, other: Self) -> Vec3<T::Output> {
        Vec3 {
            x: self.x / other.x,
            y: self.y / other.y,
            z: self.z / other.z,
        }
    }
}