Skip to content

Instantly share code, notes, and snippets.

@luqmana
Created June 17, 2013 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqmana/5794062 to your computer and use it in GitHub Desktop.
Save luqmana/5794062 to your computer and use it in GitHub Desktop.
struct Point {
x: int,
y: int
}
trait Positioned {
fn x(&self)-> int;
fn y(&self)-> int;
fn setX(&mut self, int);
fn setY(&mut self, int);
}
trait Movable: Positioned {
fn translate(&mut self, dx: int, dy: int);
}
struct Entity {
pos: Point,
}
impl Positioned for Entity {
fn x(&self)-> int {
self.pos.x
}
fn y(&self)-> int {
self.pos.y
}
fn setX(&mut self, v: int) {
self.pos.x = v;
}
fn setY(&mut self, v: int) {
self.pos.y = v;
}
}
impl Movable for Entity {
fn translate(&mut self, dx: int, dy: int) {
let x = self.x();
let y = self.y();
self.setX(x + dx);
self.setY(y + dy);
}
}
fn main() {
let mut e = Entity {
pos: Point { x: 0, y: 0 },
};
println(fmt!("%?, %?", e.x(), e.y()));
e.translate(5, 10);
println(fmt!("%?, %?", e.x(), e.y()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment