Say I have a struct PrimitiveArray<T>
that is roughly translated to an array, say stored as Vec<T>
.
Is there a way to declare the std::ops::Add
so that users can write a + b
, but where b
is not taken by value? So far I was only able to make it a + &b
:
impl<T> std::ops::Add<&PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + Add<Output = T>,
{
type Output = Self;
fn add(mut self, rhs: &Self) -> Self::Output {
binary(&mut self, rhs, |x, y| x + y);
self
}
}
- is there a trait that I can use to auto-convert so that users can write
a + b
? - Would this be a good idea (instead of making it explicit that the op is being called by ref on the right hand side?