Skip to content

Instantly share code, notes, and snippets.

@greister
Created April 18, 2016 15:57
Show Gist options
  • Save greister/7f7999e59144d8a01741a695acd0ced4 to your computer and use it in GitHub Desktop.
Save greister/7f7999e59144d8a01741a695acd0ced4 to your computer and use it in GitHub Desktop.
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