Created
April 16, 2013 20:09
-
-
Save ScatteredRay/5399184 to your computer and use it in GitHub Desktop.
rust macro
This file contains 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
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