lib.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use specs::join::Join;
  2. struct Pos {
  3. x: usize,
  4. }
  5. impl specs::Component for Pos {
  6. type Storage = specs::VecStorage<Pos>;
  7. }
  8. struct Mov;
  9. impl specs::Component for Mov {
  10. type Storage = specs::VecStorage<Mov>;
  11. }
  12. macro_rules! system {
  13. ($name:ident $pat:tt => $block:block ) => {
  14. struct $name;
  15. impl<'a> specs::System<'a> for $name {
  16. type SystemData = args_to_systemdata!($pat);
  17. fn run(&mut self, args_to_pat!($pat): Self::SystemData) {
  18. for args_to_pat!($pat) in args_to_join!($pat).join() {
  19. $block
  20. }
  21. }
  22. }
  23. };
  24. }
  25. macro_rules! args_to_systemdata {
  26. ( ( $name:ident : $ty:ty ) ) => {
  27. ( specs::ReadStorage<'a, $ty> ,)
  28. };
  29. ( ( mut $name:ident : $ty:ty ) ) => {
  30. ( specs::WriteStorage<'a, $ty> ,)
  31. };
  32. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  33. ( specs::ReadStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
  34. };
  35. ( ( mut $name:ident : $ty:ty , $( $tok:tt )* ) ) => {
  36. ( specs::WriteStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
  37. };
  38. }
  39. macro_rules! args_to_pat {
  40. ( ( $name:ident : $ty:ty ) ) => {
  41. ( $name ,)
  42. };
  43. ( ( mut $name:ident : $ty:ty ) ) => {
  44. ( mut $name ,)
  45. };
  46. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  47. ( $name, args_to_pat!( ( $($tok)* ) ) )
  48. };
  49. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  50. ( mut $name, args_to_pat!( ( $($tok)* ) ) )
  51. };
  52. }
  53. macro_rules! args_to_join {
  54. ( ( $name:ident : $ty:ty ) ) => {
  55. ( & $name ,)
  56. };
  57. ( ( mut $name:ident : $ty:ty ) ) => {
  58. ( &mut $name ,)
  59. };
  60. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  61. ( & $name, args_to_join!( ( $($tok)* ) ) )
  62. };
  63. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  64. ( &mut $name, args_to_join!( ( $($tok)* ) ) )
  65. };
  66. }
  67. system!{ Foo (_x: Mov, mut y: Pos) => {
  68. y.x += 1;
  69. }
  70. }