Created
April 18, 2016 15:57
-
-
Save greister/7f7999e59144d8a01741a695acd0ced4 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
use std::rc::Rc; | |
use std::ops::Deref; | |
trait HasPosition{ | |
fn get_position(&self) -> i32; | |
} | |
struct Body { | |
position: i32 | |
} | |
impl HasPosition for Body { | |
fn get_position(&self) -> i32 { | |
self.position | |
} | |
} | |
struct Force<T> where T: HasPosition { | |
apply_to: T, | |
value: i32, | |
} | |
impl<T> Force<T> where T: HasPosition { | |
fn apply(&self) { | |
println!("{}", self.value + self.apply_to.get_position()); | |
} | |
} | |
impl<T> HasPosition for Deref<Target=T> where T: HasPosition { | |
fn get_position(&self) -> i32 { | |
(*self).get_position() | |
} | |
} | |
fn main() { | |
//works | |
let body = Body { position: 50 }; | |
let force = Force { apply_to: body, value: 2 }; | |
force.apply(); | |
//Does not work | |
let rc_body:() = Rc::new(Body { position: 50 }); | |
let rc_force = Force { apply_to: rc_body, value: 2 }; | |
rc_force.apply(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment