I want to implement the + operator for my struct, but right now I have to do :
impl<'a, T: Add<Output = T> + Copy, const SIZE: usize> Add for &'a Vector<T, SIZE> {
type Output = Vector<T, SIZE>;
fn add(self, rhs: Self) -> Self::Output {
let mut i = 0;
let mut coords: [T; SIZE] = self.coords.map(|e| {
let val = (e + rhs.coords[i]);
i += 1;
return val;
});
Vector {
coords
}
}
}
impl<T: Add<Output = T> + Copy, const SIZE: usize> Add for Vector<T, SIZE> {
type Output = Vector<T, SIZE>;
fn add(self, rhs: Self) -> Self::Output {
let mut i = 0;
let mut coords: [T; SIZE] = self.coords.map(|e| {
let val = (e + rhs.coords[i]);
i += 1;
return val;
});
Vector {
coords
}
}
}
To get both a + b and &a + &b working.
Is there a way for me to only implement Add once and for the rust compiler to add & or * automatically ? Maybe by adding some trait bound ?