Skip to content

Instantly share code, notes, and snippets.

@bstrie
Created June 16, 2013 03:13
Show Gist options
  • Save bstrie/5790616 to your computer and use it in GitHub Desktop.
Save bstrie/5790616 to your computer and use it in GitHub Desktop.
struct Vec { x: float, y: float }
trait VecRhs {
fn add_to_Vec(&self, lhs: &Vec) -> Vec;
}
impl<R: VecRhs> Add<R, Vec> for Vec {
fn add(&self, rhs: &R) -> Vec {
rhs.add_to_Vec(self)
}
}
impl VecRhs for Vec {
fn add_to_Vec(&self, lhs: &Vec) -> Vec {
Vec { x:lhs.x+self.x, y:lhs.y+self.y }
}
}
impl VecRhs for float {
fn add_to_Vec(&self, lhs: &Vec) -> Vec {
Vec { x:lhs.x+*self, y:lhs.y+*self }
}
}
fn main() {
let mut foo = Vec { x: 2.0, y: 4.0 };
foo += 2.0;
println(fmt!("%?", foo));
foo += Vec { x: 1.0, y: 3.0 };
println(fmt!("%?", foo));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment