lib.rs 2.1 KB

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