rgeometry/data/vector/
add.rs

1use array_init::array_init;
2use num_traits::NumOps;
3use std::ops::Add;
4use std::ops::AddAssign;
5use std::ops::Index;
6
7use super::Vector;
8use super::VectorView;
9
10impl<T, const N: usize> Add<Vector<T, N>> for Vector<T, N>
11where
12  T: NumOps + Clone,
13{
14  type Output = Vector<T, N>;
15
16  fn add(self: Vector<T, N>, other: Vector<T, N>) -> Self::Output {
17    Vector(array_init(|i| {
18      self.0.index(i).clone() + other.0.index(i).clone()
19    }))
20  }
21}
22
23impl<T, const N: usize> Add<&Vector<T, N>> for &Vector<T, N>
24where
25  T: NumOps + Clone,
26{
27  type Output = Vector<T, N>;
28
29  fn add(self, other: &Vector<T, N>) -> Self::Output {
30    Vector(array_init(|i| self.0[i].clone() + other.0[i].clone()))
31  }
32}
33
34impl<T, const N: usize> AddAssign<Vector<T, N>> for Vector<T, N>
35where
36  T: NumOps + Clone + AddAssign,
37{
38  fn add_assign(&mut self, other: Vector<T, N>) {
39    for i in 0..N {
40      self.0[i] += other.0.index(i).clone()
41    }
42  }
43}
44
45impl<'a, T, const N: usize> Add for VectorView<'a, T, N>
46where
47  T: NumOps + Clone,
48{
49  type Output = Vector<T, N>;
50  fn add(self, other: VectorView<'a, T, N>) -> Vector<T, N> {
51    Vector(array_init(|i| self.0[i].clone() + other.0[i].clone()))
52  }
53}