diff options
author | HampusM <hampus@hampusmat.com> | 2024-04-21 15:07:59 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-04-21 15:07:59 +0200 |
commit | 26092b2824ad2d956023adf0af3f1ad0423e02ea (patch) | |
tree | b39ae736366daf82230907a8322323d8796f08cf | |
parent | a93dd51a378b29b76d256abf6270671e69464cf2 (diff) |
feat(ecs-macros): make derive macros support types with generics
-rw-r--r-- | ecs-macros/src/lib.rs | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs index dcc6d99..21a2c38 100644 --- a/ecs-macros/src/lib.rs +++ b/ecs-macros/src/lib.rs @@ -3,7 +3,17 @@ use std::path::PathBuf as FsPathBuf; use proc_macro::TokenStream; use quote::{quote, ToTokens}; use syn::spanned::Spanned; -use syn::{parse, Attribute, Ident, Item, ItemEnum, ItemStruct, ItemUnion, Path}; +use syn::{ + parse, + Attribute, + Generics, + Ident, + Item, + ItemEnum, + ItemStruct, + ItemUnion, + Path, +}; use toml::value::{Table as TomlTable, Value as TomlValue}; macro_rules! syn_path { @@ -43,10 +53,13 @@ pub fn component_derive(input: TokenStream) -> TokenStream let drop_last = component_attr.drop_last; + let (impl_generics, type_generics, where_clause) = item.generics().split_for_impl(); + let ecs_path = find_engine_ecs_crate_path().unwrap_or_else(|| syn_path!(ecs)); quote! { - impl #ecs_path::component::Component for #item_ident + impl #impl_generics #ecs_path::component::Component for #item_ident #type_generics + #where_clause { fn drop_last(&self) -> bool { @@ -64,9 +77,13 @@ pub fn component_derive(input: TokenStream) -> TokenStream } } - impl #ecs_path::system::Input for #item_ident {} + impl #impl_generics #ecs_path::system::Input for #item_ident #type_generics + #where_clause + { + } - impl #ecs_path::type_name::TypeName for #item_ident + impl #impl_generics #ecs_path::type_name::TypeName for #item_ident #type_generics + #where_clause { fn type_name(&self) -> &'static str { @@ -88,10 +105,13 @@ pub fn sole_derive(input: TokenStream) -> TokenStream let drop_last = sole_attr.drop_last; + let (impl_generics, type_generics, where_clause) = item.generics().split_for_impl(); + let ecs_path = find_engine_ecs_crate_path().unwrap_or_else(|| syn_path!(ecs)); quote! { - impl #ecs_path::sole::Sole for #item_ident + impl #impl_generics #ecs_path::sole::Sole for #item_ident #type_generics + #where_clause { fn drop_last(&self) -> bool { @@ -109,7 +129,8 @@ pub fn sole_derive(input: TokenStream) -> TokenStream } } - impl #ecs_path::type_name::TypeName for #item_ident + impl #impl_generics #ecs_path::type_name::TypeName for #item_ident #type_generics + #where_clause { fn type_name(&self) -> &'static str { @@ -165,6 +186,15 @@ impl TypeItem Some(Attr::from_attribute(attr?).unwrap()) } + + fn generics(&self) -> &Generics + { + match self { + Self::Struct(struct_item) => &struct_item.generics, + Self::Enum(enum_item) => &enum_item.generics, + Self::Union(union_item) => &union_item.generics, + } + } } impl TryFrom<Item> for TypeItem |