Browse Source

Add system_impl macro as well

Getty Ritter 4 years ago
parent
commit
35f0b153f2
1 changed files with 57 additions and 0 deletions
  1. 57 0
      src/lib.rs

+ 57 - 0
src/lib.rs

@@ -25,6 +25,20 @@ macro_rules! system {
     };
 }
 
+#[macro_export]
+macro_rules! system_impl {
+    ($name:ident ( $($pat:tt)* ) { $($rest:tt)* } ) => {
+        pub struct $name;
+        impl<'a> specs::System<'a> for $name {
+            type SystemData = args_to_systemdata!(($($pat)*,));
+            fn run(&mut self, args_to_fn_pat!(($($pat)*)): Self::SystemData) {
+                use specs::join::Join;
+                $($rest)*
+            }
+        }
+    };
+}
+
 #[macro_export]
 macro_rules! args_to_systemdata {
     ( ( $name:ident : $ty:ty $(,)? ) ) => {
@@ -160,6 +174,14 @@ mod tests {
         }
     }
 
+    system_impl! {
+        FooAlt (x: Mov, mut y: Pos) {
+            for (_, mut y) in (&x, &mut y).join() {
+                y.x += 1;
+            }
+        }
+    }
+
     system! {
         Bar (_x: Mov, mut y: Pos) {
             y.x += 1;
@@ -168,10 +190,45 @@ mod tests {
         }
     }
 
+    system_impl! {
+        BarAlt (x: Mov, mut y: Pos) {
+            for (_, mut y) in (&x, &mut y).join() {
+                y.x += 1;
+            }
+            println!("Done!");
+        }
+    }
+
     system! {
         Baz (resource n: usize, _x: Mov, mut y: Pos) {
             y.x += *n;
         }
     }
 
+    system_impl! {
+        BazAlt (resource n: usize, x: Mov, mut y: Pos) {
+            for (_, mut y) in (&x, &mut y).join() {
+                y.x += *n;
+            }
+        }
+    }
+
+    system! {
+        Quux (resource mut n: usize, _x: Mov, mut y: Pos) {
+            y.x += *n;
+            *n += 1;
+        } finally {
+            *n += 1;
+        }
+    }
+
+    system_impl! {
+        QuuxAlt (resource mut n: usize, x: Mov, mut y: Pos) {
+            for (_, mut y) in (&x, &mut y).join() {
+                y.x += *n;
+            }
+            *n += 1;
+        }
+    }
+
 }