Create a Component derive macro

This commit is contained in:
SeanOMik 2023-12-21 17:02:59 -05:00
parent 1ab79fae83
commit 1a541e527b
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
7 changed files with 55 additions and 5 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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
}
}
})
}

View File

@ -1,3 +1,7 @@
/// Shorthand for `Send + Sync + 'static`, so it never needs to be implemented manually.
pub trait Component: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> Component for T {}
/* pub trait Component: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> Component for T {} */
pub trait Component: 'static {
fn name() -> &'static str;
}

View File

@ -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;

View File

@ -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,

View File

@ -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()