Last active
May 22, 2018 08:51
-
-
Save kazimuth/8e6a65e0cba4c87773a5a0769452eb11 to your computer and use it in GitHub Desktop.
System impl macro for specs
This file contains hidden or 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
use specs::System; | |
// Tired of writing out a bunch of giant tuples by hand? | |
// I have the macro for you! | |
struct BananaSystem; | |
system! { | |
BananaSystem; | |
fn run(&mut self, | |
comp_a: ReadStorage<ComponentA>, | |
mut comp_b: WriteStorage<ComponentB>, | |
mut comp_c: WriteStorage<ComponentC>) { | |
for (a, mut b, mut c) in (comp_a, comp_b, comp_c).join() { | |
// etc. | |
} | |
} | |
} | |
// will generate what you'd expect (appropriate SystemData, etc.) | |
// Here's the macro definition | |
macro_rules! system { | |
( | |
$name:path; | |
fn run(&mut self, $( | |
$($bind:ident)+ : | |
$type_root:ident < $( $type_args:ty ),* > | |
),+ ) | |
$contents:block | |
) => { | |
impl<'a> System<'a> for $name { | |
type SystemData = ( $( | |
$type_root <'a, $( $type_args ),* > | |
),* ); | |
fn run(&mut self, args: Self::SystemData) { | |
system_maybe_tuple!( args, $($($bind)+),+ ); | |
$contents | |
} | |
} | |
}; | |
} | |
macro_rules! system_maybe_tuple { | |
($args:expr, $($bind:ident)+) => ( | |
let ($($bind)+,) = $args; | |
); | |
($args:expr, $($($bind:ident)+),+) => ( | |
let ($($($bind)+),+) = $args; | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment