2 // If `Mul` used an associated type for its output, this test would
13 // methods we want to export as methods as well as operators
16 fn vmul(self, other: f64) -> Vec2 {
17 Vec2 { x: self.x * other, y: self.y * other }
21 // Right-hand-side operator visitor pattern
25 fn mul_vec2_by(&self, lhs: &Vec2) -> Self::Result;
28 // Vec2's implementation of Mul "from the other side" using the above trait
29 impl<Res, Rhs: RhsOfVec2Mul<Result=Res>> Mul<Rhs> for Vec2 {
32 fn mul(self, rhs: Rhs) -> Res { rhs.mul_vec2_by(&self) }
35 // Implementation of 'f64 as right-hand-side of Vec2::Mul'
36 impl RhsOfVec2Mul for f64 {
39 fn mul_vec2_by(&self, lhs: &Vec2) -> Vec2 { lhs.vmul(*self) }
42 // Usage with failing inference
44 let a = Vec2 { x: 3.0f64, y: 4.0f64 };
46 // the following compiles and works properly
47 let v1: Vec2 = a * 3.0f64;
48 println!("{} {}", v1.x, v1.y);
50 // the following compiles but v2 will not be Vec2 yet and
51 // using it later will cause an error that the type of v2
54 println!("{} {}", v2.x, v2.y); // error regarding v2's type