Skip to main content

vector_x/
vector3.rs

1use std::fmt::Display;
2
3use crate::Vector2;
4
5/// A struct that comprises of three elements
6pub struct Vector3<T> {
7    one: T,
8    two: T,
9    three: T
10}
11
12impl<T> Vector3<T> {
13    pub fn new(one: T, two: T, three: T) -> Self {
14        return Self {
15            one: one,
16            two: two,
17            three: three
18        };
19    }
20}
21
22impl<T> Vector3<T> {
23    pub fn get_one(&self) -> &T {
24        return &self.one;
25    }
26
27    pub fn get_one_mut(&mut self) -> &mut T {
28        return &mut self.one;
29    }
30
31    pub fn get_two(&self) -> &T {
32        return &self.two;
33    }
34
35    pub fn get_two_mut(&mut self) -> &mut T {
36        return &mut self.two;
37    }
38
39    pub fn get_three(&self) -> &T {
40        return &self.three;
41    }
42
43    pub fn get_three_mut(&mut self) -> &mut T {
44        return &mut self.three;
45    }
46}
47
48impl<T: Default> Default for Vector3<T> {
49    fn default() -> Self {
50        return Self {
51            one: T::default(),
52            two: T::default(),
53            three: T::default()
54        };
55    }
56}
57
58impl<T: Clone> Clone for Vector3<T> {
59    fn clone(&self) -> Self {
60        return Self {
61            one: self.one.clone(),
62            two: self.two.clone(),
63            three: self.three.clone()
64        };
65    }
66}
67
68impl<T: Copy> Copy for Vector3<T> {}
69
70impl<T> Into<(T, T, T)> for Vector3<T> {
71    fn into(self) -> (T, T, T) {
72        return (self.one, self.two, self.three);
73    }
74}
75
76impl<T> Into<Vector3<T>> for (T, T, T) {
77    fn into(self) -> Vector3<T> {
78        return Vector3::new(self.0, self.1, self.2);
79    }
80}
81
82impl<T: PartialEq> PartialEq for Vector3<T> {
83    fn eq(&self, other: &Self) -> bool {
84        return self.one == other.one && 
85            self.two == other.two &&
86            self.three == other.three;
87    }
88    
89    fn ne(&self, other: &Self) -> bool {
90        return !self.eq(other);
91    }
92}
93
94impl<T> Into<(Vector2<T>, T)> for Vector3<T> {
95    fn into(self) -> (Vector2<T>, T) {
96        return (Vector2::new(self.one, self.two), self.three);
97    }
98}
99
100impl<T> Into<Vector3<T>> for (Vector2<T>, T) {
101    fn into(self) -> Vector3<T> {
102        let (v1, v2) = self.0.into();
103        return Vector3::new(v1, v2, self.1);
104    }
105}
106
107impl<T: Display> Display for Vector3<T> {
108    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109        return write!(f, "[one: {}, two: {}, three: {}", self.one, self.two, self.three);
110    }
111}