Skip to content

Instantly share code, notes, and snippets.

@ScatteredRay
Created April 16, 2013 20:09
Show Gist options
  • Save ScatteredRay/5399184 to your computer and use it in GitHub Desktop.
Save ScatteredRay/5399184 to your computer and use it in GitHub Desktop.
rust macro
trait Persistable {
fn persist_test(&self) -> int;
}
fn persist<T: Persistable>(val : T) {
io::println(fmt!("Val: %d", val.persist_test()));
}
macro_rules! object(
($cname:ident { $($tname:ident : $typ:ty),* }) => (
struct $cname {
$( $tname : $typ),*
}
impl Persistable for $cname {
fn persist_test(&self) -> int {
return self.x;
}
}
)
)
object!(vec3 {
x : int,
y : int,
z : int
})
fn main() {
let v = vec3{x: 1, y: 2, z: 3};
persist(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment