Created
June 16, 2013 03:13
-
-
Save bstrie/5790616 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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