Guest User

Untitled

a guest
May 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. use specs::System;
  2.  
  3. // Tired of writing out a bunch of giant tuples by hand?
  4. // I have the macro for you!
  5.  
  6. struct BananaSystem;
  7.  
  8. system! {
  9. BananaSystem;
  10.  
  11. fn run(&mut self,
  12. comp_a: ReadStorage<ComponentA>,
  13. mut comp_b: WriteStorage<ComponentB>,
  14. mut comp_c: WriteStorage<ComponentC>) {
  15. for (a, mut b, mut c) in (comp_a, comp_b, comp_c).join() {
  16. // etc.
  17. }
  18. }
  19. }
  20. // will generate what you'd expect (appropriate SystemData, etc.)
  21.  
  22. // Here's the macro definition
  23. macro_rules! system {
  24. (
  25. $name:path;
  26.  
  27. fn run(&mut self, $(
  28. $($bind:ident)+ :
  29. $type_root:ident < $( $type_args:ty ),* >
  30. ),+ )
  31. $contents:block
  32.  
  33. ) => {
  34.  
  35. impl<'a> System<'a> for $name {
  36. type SystemData = ( $(
  37. $type_root <'a, $( $type_args ),* >
  38. ),* );
  39.  
  40. fn run(&mut self, args: Self::SystemData) {
  41. system_maybe_tuple!( args, $($($bind)+),+ );
  42. $contents
  43. }
  44. }
  45. };
  46. }
  47. macro_rules! system_maybe_tuple {
  48. ($args:expr, $($bind:ident)+) => (
  49. let ($($bind)+,) = $args;
  50. );
  51. ($args:expr, $($($bind:ident)+),+) => (
  52. let ($($($bind)+),+) = $args;
  53. );
  54. }
Add Comment
Please, Sign In to add comment