1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct Vector<T = f32> {
4 pub x: T,
6
7 pub y: T,
9}
10
11impl<T> Vector<T> {
12 pub const fn new(x: T, y: T) -> Self {
14 Self { x, y }
15 }
16}
17
18impl Vector {
19 pub const ZERO: Self = Self::new(0.0, 0.0);
21}
22
23impl<T> std::ops::Neg for Vector<T>
24where
25 T: std::ops::Neg<Output = T>,
26{
27 type Output = Self;
28
29 fn neg(self) -> Self::Output {
30 Self::new(-self.x, -self.y)
31 }
32}
33
34impl<T> std::ops::Add for Vector<T>
35where
36 T: std::ops::Add<Output = T>,
37{
38 type Output = Self;
39
40 fn add(self, b: Self) -> Self {
41 Self::new(self.x + b.x, self.y + b.y)
42 }
43}
44
45impl<T> std::ops::Sub for Vector<T>
46where
47 T: std::ops::Sub<Output = T>,
48{
49 type Output = Self;
50
51 fn sub(self, b: Self) -> Self {
52 Self::new(self.x - b.x, self.y - b.y)
53 }
54}
55
56impl<T> std::ops::Mul<T> for Vector<T>
57where
58 T: std::ops::Mul<Output = T> + Copy,
59{
60 type Output = Self;
61
62 fn mul(self, scale: T) -> Self {
63 Self::new(self.x * scale, self.y * scale)
64 }
65}
66
67impl<T> Default for Vector<T>
68where
69 T: Default,
70{
71 fn default() -> Self {
72 Self {
73 x: T::default(),
74 y: T::default(),
75 }
76 }
77}
78
79impl<T> From<[T; 2]> for Vector<T> {
80 fn from([x, y]: [T; 2]) -> Self {
81 Self::new(x, y)
82 }
83}
84
85impl<T> From<Vector<T>> for [T; 2]
86where
87 T: Copy,
88{
89 fn from(other: Vector<T>) -> Self {
90 [other.x, other.y]
91 }
92}