Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2016 12:26
Show Gist options
  • Save anonymous/536588a51d875532d84f416fa3d912e2 to your computer and use it in GitHub Desktop.
Save anonymous/536588a51d875532d84f416fa3d912e2 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
macro_rules! item {
( $i:item ) => ( #[derive(Debug)] $i )
}
macro_rules! vertex_struct {
( $name:ident { $( $n:ident : $t:ty ),* }) => ( item! { struct $name { $( $n: $t ),* } })
}
macro_rules! constant_struct {
( $name:ident $( $body:tt )* ) => ( item! { struct $name $( $body )* })
}
macro_rules! pipeline_struct {
( $name:ident $( $body:tt )* ) => ( item! { struct $name $( $body )* })
}
macro_rules! dispatch {
( vertex struct $name:ident $body:tt )
=> ( vertex_struct! { $name $body } );
( constant struct $name:ident $body:tt )
=> ( constant_struct! { $name $body } );
( pipeline struct $name:ident $body:tt )
=> ( pipeline_struct! { $name $body } );
}
macro_rules! defs {
( $( $modifier:tt struct $name:ident $body:tt )* )
=> ( $( dispatch! { $modifier struct $name $body } )* )
}
defs! {
vertex struct Vtx { p: [f32; 2], c: [f32; 3] }
pipeline struct Pipe { vbuf: Vtx }
}
fn main() {
let pipe = Pipe { vbuf: Vtx { p: [ 1.5, 2.5 ], c: [ 0.0, 0.0, 0.0 ] } };
println!("{:?}", pipe);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment