lib.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #[macro_export]
  2. macro_rules! system {
  3. ($name:ident $pat:tt { $($rest:tt)* } ) => {
  4. pub 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. ($name:ident $pat:tt { $($rest:tt)* } finally { $($finally:tt)* }) => {
  15. pub struct $name;
  16. impl<'a> specs::System<'a> for $name {
  17. type SystemData = args_to_systemdata!($pat);
  18. fn run(&mut self, args_to_pat!($pat): Self::SystemData) {
  19. for args_to_pat!($pat) in args_to_join!($pat).join() {
  20. $($rest)*
  21. }
  22. $($finally)*
  23. }
  24. }
  25. };
  26. }
  27. #[macro_export]
  28. macro_rules! args_to_systemdata {
  29. ( ( $name:ident : $ty:ty $(,)? ) ) => {
  30. ( specs::ReadStorage<'a, $ty> ,)
  31. };
  32. ( ( mut $name:ident : $ty:ty $(,)? ) ) => {
  33. ( specs::WriteStorage<'a, $ty> ,)
  34. };
  35. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  36. ( specs::ReadStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
  37. };
  38. ( ( mut $name:ident : $ty:ty , $( $tok:tt )* ) ) => {
  39. ( specs::WriteStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
  40. };
  41. }
  42. #[macro_export]
  43. macro_rules! args_to_pat {
  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_pat!( ( $($tok)* ) ) )
  52. };
  53. ( ( mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  54. ( mut $name, args_to_pat!( ( $($tok)* ) ) )
  55. };
  56. }
  57. #[macro_export]
  58. macro_rules! args_to_join {
  59. ( ( $name:ident : $ty:ty $(,)? ) ) => {
  60. ( & $name ,)
  61. };
  62. ( ( mut $name:ident : $ty:ty $(,)? ) ) => {
  63. ( &mut $name ,)
  64. };
  65. ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  66. ( & $name, args_to_join!( ( $($tok)* ) ) )
  67. };
  68. ( ( mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
  69. ( &mut $name, args_to_join!( ( $($tok)* ) ) )
  70. };
  71. }
  72. #[cfg(test)]
  73. mod tests {
  74. use specs::join::Join;
  75. struct Pos {
  76. x: usize,
  77. }
  78. impl specs::Component for Pos {
  79. type Storage = specs::VecStorage<Pos>;
  80. }
  81. struct Mov;
  82. impl specs::Component for Mov {
  83. type Storage = specs::VecStorage<Mov>;
  84. }
  85. system! { Foo (_x: Mov, mut y: Pos) {
  86. y.x += 1;
  87. }
  88. }
  89. system! { Bar (_x: Mov, mut y: Pos) {
  90. y.x += 1;
  91. } finally {
  92. println!("Done!");
  93. }
  94. }
  95. }