1use std::ops::{Add, Sub, Mul, Div};
28
29#[repr(C)]
31#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Copy)]
32pub struct Vector2<T> {
33 pub x: T,
35 pub y: T
37}
38
39pub type Vector2i = Vector2<i32>;
41pub type Vector2u = Vector2<u32>;
43pub type Vector2f = Vector2<f32>;
45
46impl<T> Vector2<T> {
47 pub fn new(x: T, y: T) -> Vector2<T> {
49 Vector2 {
50 x: x,
51 y: y
52 }
53 }
54}
55
56impl<T: Add + Copy> Add<T> for Vector2<T> {
57 type Output = Vector2<T::Output>;
58
59 fn add(self, rhs: T) -> Vector2<T::Output> {
60 Vector2 {
61 x: self.x + rhs,
62 y: self.y + rhs
63 }
64 }
65}
66
67impl<T: Sub + Copy> Sub<T> for Vector2<T> {
68 type Output = Vector2<T::Output>;
69
70 fn sub(self, rhs: T) -> Vector2<T::Output> {
71 Vector2 {
72 x: self.x - rhs,
73 y: self.y - rhs
74 }
75 }
76}
77
78impl<T: Mul + Copy> Mul<T> for Vector2<T> {
79 type Output = Vector2<T::Output>;
80
81 fn mul(self, rhs: T) -> Vector2<T::Output> {
82 Vector2 {
83 x: self.x * rhs,
84 y: self.y * rhs
85 }
86 }
87}
88
89impl<T: Div + Copy> Div<T> for Vector2<T> {
90 type Output = Vector2<T::Output>;
91
92 fn div(self, rhs: T) -> Vector2<T::Output> {
93 Vector2 {
94 x: self.x / rhs,
95 y: self.y / rhs
96 }
97 }
98}
99
100
101impl<T: Add> Add for Vector2<T> {
102 type Output = Vector2<T::Output>;
103
104 fn add(self, rhs: Vector2<T>) -> Vector2<T::Output> {
105 Vector2 {
106 x: self.x + rhs.x,
107 y: self.y + rhs.y
108 }
109 }
110}
111
112impl<T: Sub> Sub for Vector2<T> {
113 type Output = Vector2<T::Output>;
114
115 fn sub(self, rhs: Vector2<T>) -> Vector2<T::Output> {
116 Vector2 {
117 x: self.x - rhs.x,
118 y: self.y - rhs.y
119 }
120 }
121}
122
123impl<T: Mul> Mul for Vector2<T> {
124 type Output = Vector2<T::Output>;
125
126 fn mul(self, rhs: Vector2<T>) -> Vector2<T::Output> {
127 Vector2 {
128 x: self.x * rhs.x,
129 y: self.y * rhs.y
130 }
131 }
132}
133
134impl<T: Div> Div for Vector2<T> {
135 type Output = Vector2<T::Output>;
136
137 fn div(self, rhs: Vector2<T>) -> Vector2<T::Output> {
138 Vector2 {
139 x: self.x / rhs.x,
140 y: self.y / rhs.y
141 }
142 }
143}
144
145
146pub trait ToVec {
148 fn to_vector2f(&self) -> Vector2f;
150 fn to_vector2i(&self) -> Vector2i;
152 fn to_vector2u(&self) -> Vector2u;
154}
155
156impl ToVec for Vector2f {
157 fn to_vector2f(&self) -> Vector2f {
158 self.clone()
159 }
160
161 fn to_vector2i(&self) -> Vector2i {
162 Vector2i {
163 x: self.x as i32,
164 y: self.y as i32
165 }
166 }
167
168 fn to_vector2u(&self) -> Vector2u {
169 Vector2u {
170 x: self.x as u32,
171 y: self.y as u32
172 }
173 }
174}
175
176impl ToVec for Vector2i {
177 fn to_vector2f(&self) -> Vector2f {
178 Vector2f {
179 x: self.x as f32,
180 y: self.y as f32
181 }
182 }
183
184 fn to_vector2i(&self) -> Vector2i {
185 self.clone()
186 }
187
188 fn to_vector2u(&self) -> Vector2u {
189 Vector2u {
190 x: self.x as u32,
191 y: self.y as u32
192 }
193 }
194}
195
196impl ToVec for Vector2u {
197 fn to_vector2f(&self) -> Vector2f {
198 Vector2f {
199 x: self.x as f32,
200 y: self.y as f32
201 }
202 }
203
204 fn to_vector2i(&self) -> Vector2i {
205 Vector2i {
206 x: self.x as i32,
207 y: self.y as i32
208 }
209 }
210
211 fn to_vector2u(&self) -> Vector2u {
212 self.clone()
213 }
214}