diff --git a/lyra-ecs/Cargo.toml b/lyra-ecs/Cargo.toml index bac5140..5c9afd2 100644 --- a/lyra-ecs/Cargo.toml +++ b/lyra-ecs/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +lyra-ecs-derive = { path = "./lyra-ecs-derive" } anyhow = "1.0.75" thiserror = "1.0.50" diff --git a/lyra-ecs/lyra-ecs-derive/Cargo.toml b/lyra-ecs/lyra-ecs-derive/Cargo.toml new file mode 100644 index 0000000..04fc6af --- /dev/null +++ b/lyra-ecs/lyra-ecs-derive/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "lyra-ecs-derive" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0.70" +quote = "1.0.33" +syn = "2.0.41" \ No newline at end of file diff --git a/lyra-ecs/lyra-ecs-derive/src/lib.rs b/lyra-ecs/lyra-ecs-derive/src/lib.rs new file mode 100644 index 0000000..bf56636 --- /dev/null +++ b/lyra-ecs/lyra-ecs-derive/src/lib.rs @@ -0,0 +1,24 @@ +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + + +#[proc_macro_derive(Component)] +pub fn derive_component(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let input = parse_macro_input!(input as DeriveInput); + + //let generics = input.generics.clone() + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + + let type_ident = &input.ident; + //type_ident.span(). + + let type_name = type_ident.to_string(); + + proc_macro::TokenStream::from(quote! { + impl #impl_generics ::lyra_ecs::Component for #type_ident #ty_generics #where_clause { + fn name() -> &'static str { + #type_name + } + } + }) +} \ No newline at end of file diff --git a/lyra-ecs/src/component.rs b/lyra-ecs/src/component.rs index af3a9e0..8cb096e 100644 --- a/lyra-ecs/src/component.rs +++ b/lyra-ecs/src/component.rs @@ -1,3 +1,7 @@ /// Shorthand for `Send + Sync + 'static`, so it never needs to be implemented manually. -pub trait Component: Send + Sync + 'static {} -impl Component for T {} \ No newline at end of file +/* pub trait Component: Send + Sync + 'static {} +impl Component for T {} */ + +pub trait Component: 'static { + fn name() -> &'static str; +} \ No newline at end of file diff --git a/lyra-ecs/src/lib.rs b/lyra-ecs/src/lib.rs index 008adcb..43c1f6c 100644 --- a/lyra-ecs/src/lib.rs +++ b/lyra-ecs/src/lib.rs @@ -1,5 +1,8 @@ use crate::world::World; +extern crate self as lyra_ecs; +//mod lyra_ecs { pub use super::*; } + mod archetype; pub use archetype::*; @@ -24,6 +27,8 @@ pub use resource::*; mod system; pub use system::*; +pub use lyra_ecs_derive::*; + #[cfg(test)] mod tests; diff --git a/lyra-ecs/src/tests.rs b/lyra-ecs/src/tests.rs index aa33648..30e81dc 100644 --- a/lyra-ecs/src/tests.rs +++ b/lyra-ecs/src/tests.rs @@ -1,8 +1,10 @@ +use lyra_ecs_derive::Component; use rand::Rng; +//use crate::lyra_ecs; /// This source file includes some common things that tests are using. -#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)] +#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, Component)] pub struct Vec2 { pub x: f32, pub y: f32, @@ -27,7 +29,7 @@ impl Vec2 { } } -#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)] +#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, Component)] pub struct Vec3 { pub x: f32, pub y: f32, diff --git a/lyra-ecs/src/world.rs b/lyra-ecs/src/world.rs index 4970685..36b5515 100644 --- a/lyra-ecs/src/world.rs +++ b/lyra-ecs/src/world.rs @@ -181,7 +181,7 @@ impl World { } /// View into the world for a set of entities that satisfy the queries. - pub fn view_iter<'a, T: 'static + Component + AsQuery>(&'a self) -> ViewIter<'a, T::Query> { + pub fn view_iter<'a, T: 'static + AsQuery>(&'a self) -> ViewIter<'a, T::Query> { let archetypes = self.archetypes.values().collect(); let v = View::new(self, T::Query::new(), archetypes); v.into_iter()