Browse Source

Basic attempt at specs system macro

Getty Ritter 2 years ago
commit
ee7c941b17
3 changed files with 70 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 10 0
      Cargo.toml
  3. 57 0
      src/lib.rs

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/target
+**/*.rs.bk
+Cargo.lock

+ 10 - 0
Cargo.toml

@@ -0,0 +1,10 @@
+[package]
+name = "specs-system-derive"
+version = "0.1.0"
+authors = ["Getty Ritter <gettylefou@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+specs = "*"

+ 57 - 0
src/lib.rs

@@ -0,0 +1,57 @@
+use specs::join::Join;
+
+struct Pos {
+    x: usize,
+}
+impl specs::Component for Pos {
+    type Storage = specs::VecStorage<Pos>;
+}
+struct Mov;
+impl specs::Component for Mov {
+    type Storage = specs::VecStorage<Mov>;
+}
+
+macro_rules! system {
+    ($name:ident $pat:tt => $block:block ) => {
+        struct $name;
+        impl<'a> specs::System<'a> for $name {
+            type SystemData = args_to_systemdata!($pat);
+            fn run(&mut self, args_to_pat!($pat): Self::SystemData) {
+                for args_to_pat!($pat) in (args_to_join!($pat)).join() {
+                    $block
+                }
+            }
+        }
+    };
+}
+
+macro_rules! args_to_systemdata {
+    ( ( $( $name:ident : $ty: ty ),* ) ) => {
+        ( $( specs::WriteStorage<'a, $ty> ),* )
+    }
+}
+
+macro_rules! args_to_pat {
+    ( ( $( $name:ident : $ty: ty ),* ) ) => {
+        ( $( mut $name ),* )
+    }
+}
+
+macro_rules! args_to_join {
+    ( ( $( $name:ident : $ty: ty ),* ) ) => {
+        ( $( &mut $name ),* )
+    }
+}
+
+system!{ Foo (_x: Mov, y: Pos) => {
+    y.x += 1;
+}
+}
+
+#[cfg(test)]
+mod tests {
+    #[test]
+    fn it_works() {
+        assert_eq!(2 + 2, 4);
+    }
+}