Hi Rustaceans,
For trait and generic exploration, I'm writing a basic Vector2D code. The struct is quite simple:
#[derive(Debug, Copy, Clone)]
pub struct Vector2D<T> {
pub x: T,
pub y: T,
}
I managed to implement Add, Sub and Mul *T operation (vector * T) , but I'm unable to implement T * vector . Here is my implementation for Vec * T operation:
impl<T: Copy + std::ops::Mul<Output=T>> ops::Mul<T> for Vector2D<T> {
type Output = Vector2D<T>;
fn mul(self, scale: T) > Self::Output {
Vector2D { x: self.x * scale, y: self.y * scale }
}
}
I tried many things, but the best I can do is:
impl<T: Copy + std::ops::Mul<Output=T>> ops::Mul< Vector2D<T>> for T {
type Output = Vector2D<T>;
fn mul(self, v: Vector2D<T>) > Self::Output {
Vector2D { x: self * v.x, y: self * v.y }
}
}
which leads to the following error:
error[E0210]: type parameter `T` must be covered by another type when it appears before the first local type (`vector::Vector2D<T>`)
> src/vector.rs:87:6

87  impl<T: Copy + std::ops::Mul<Output=T>> ops::Mul< Vector2D<T>> for T {
 ^ type parameter `T` must be covered by another type when it appears before the first local type (`vector::Vector2D<T>`)

= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
= note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last
I spent too much time on this, and now I'm really confused. The error message make me think to something related to the Â´Orphan Rule', but I'm not sure about that. Now, but I'm asking if I'm not trying to do something impossible ?
Best regards