lib.rs 2.0 KB

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