1use std::ops::{Add, Sub, Mul, Div};
28
29#[repr(C)]
31#[derive(Clone, PartialOrd, PartialEq, Debug, Copy)]
32pub struct Vector3<T> {
33 pub x: T,
35 pub y: T,
37 pub z: T
39}
40
41impl<T> Vector3<T> {
42 pub fn new(x: T, y: T, z: T) -> Vector3<T> {
44 Vector3 {
45 x: x,
46 y: y,
47 z: z
48 }
49 }
50}
51
52pub type Vector3f = Vector3<f32>;
54pub type Vector3i = Vector3<i32>;
56pub type Vector3u = Vector3<u32>;
58
59impl<T: Add + Copy> Add<T> for Vector3<T> {
60 type Output = Vector3<T::Output>;
61
62 fn add(self, rhs: T) -> Vector3<T::Output> {
63 Vector3 {
64 x: self.x + rhs,
65 y: self.y + rhs,
66 z: self.z + rhs
67 }
68 }
69}
70
71impl<T: Sub + Copy> Sub<T> for Vector3<T> {
72 type Output = Vector3<T::Output>;
73
74 fn sub(self, rhs: T) -> Vector3<T::Output> {
75 Vector3 {
76 x: self.x - rhs,
77 y: self.y - rhs,
78 z: self.z - rhs
79 }
80 }
81}
82
83impl<T: Mul + Copy> Mul<T> for Vector3<T> {
84 type Output = Vector3<T::Output>;
85
86 fn mul(self, rhs: T) -> Vector3<T::Output> {
87 Vector3 {
88 x: self.x * rhs,
89 y: self.y * rhs,
90 z: self.z * rhs
91 }
92 }
93}
94
95impl<T: Div + Copy> Div<T> for Vector3<T> {
96 type Output = Vector3<T::Output>;
97
98 fn div(self, rhs: T) -> Vector3<T::Output> {
99 Vector3 {
100 x: self.x / rhs,
101 y: self.y / rhs,
102 z: self.z / rhs
103 }
104 }
105}
106
107
108impl<T: Add> Add for Vector3<T> {
109 type Output = Vector3<T::Output>;
110
111 fn add(self, rhs: Vector3<T>) -> Vector3<T::Output> {
112 Vector3 {
113 x: self.x + rhs.x,
114 y: self.y + rhs.y,
115 z: self.z + rhs.z
116 }
117 }
118}
119
120impl<T: Sub> Sub for Vector3<T> {
121 type Output = Vector3<T::Output>;
122
123 fn sub(self, rhs: Vector3<T>) -> Vector3<T::Output> {
124 Vector3 {
125 x: self.x - rhs.x,
126 y: self.y - rhs.y,
127 z: self.z - rhs.z
128 }
129 }
130}
131
132impl<T: Mul> Mul for Vector3<T> {
133 type Output = Vector3<T::Output>;
134
135 fn mul(self, rhs: Vector3<T>) -> Vector3<T::Output> {
136 Vector3 {
137 x: self.x * rhs.x,
138 y: self.y * rhs.y,
139 z: self.z * rhs.z
140 }
141 }
142}
143
144impl<T: Div> Div for Vector3<T> {
145 type Output = Vector3<T::Output>;
146
147 fn div(self, rhs: Vector3<T>) -> Vector3<T::Output> {
148 Vector3 {
149 x: self.x / rhs.x,
150 y: self.y / rhs.y,
151 z: self.z / rhs.z
152 }
153 }
154}