diff options
| -rw-r--r-- | ecs-macros/src/lib.rs | 50 | 
1 files changed, 10 insertions, 40 deletions
| diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs index 647469b..a6fdf28 100644 --- a/ecs-macros/src/lib.rs +++ b/ecs-macros/src/lib.rs @@ -6,7 +6,6 @@ use syn::spanned::Spanned;  use syn::{      parse,      Attribute, -    GenericParam,      Generics,      Ident,      Item, @@ -57,45 +56,17 @@ pub fn component_derive(input: TokenStream) -> TokenStream      let ecs_path = find_engine_ecs_crate_path().unwrap_or_else(|| syn_path!(ecs)); -    let (id_or_ids, get_id) = if !item.generics().params.is_empty() { -        let id_lut_ident = -            format_ident!("{}_ID_LUT", item_ident.to_string().to_uppercase()); +    assert!( +        item.generics().params.is_empty(), +        "Generic types are not supported as components" +    ); -        let id_lut = quote! { -            static #id_lut_ident: LazyLock<Mutex<HashMap<TypeId, Uid>>> = -                LazyLock::new(|| Mutex::new(HashMap::new())); -        }; +    let id_var_ident = format_ident!("{}_ID", item_ident.to_string().to_uppercase()); -        let generics = item.generics().params.iter().map(|param| match param { -            GenericParam::Type(type_param) => type_param.ident.clone(), -            GenericParam::Lifetime(_) => panic!("Lifetime generics are not supported"), -            GenericParam::Const(_) => panic!("Const generics are not supported"), +    let id_var = quote! { +        static #id_var_ident: LazyLock<Uid> = LazyLock::new(|| { +            Uid::new_unique(UidKind::Component)          }); - -        let get_id = quote! { -            *#id_lut_ident -                .try_lock() -                .unwrap() -                .entry(TypeId::of::<(#(#generics,)*)>()) -                .or_insert_with(|| Uid::new_unique(UidKind::Component)) -        }; - -        (id_lut, get_id) -    } else { -        let id_lazylock_ident = -            format_ident!("{}_ID", item_ident.to_string().to_uppercase()); - -        let id_lazylock = quote! { -            static #id_lazylock_ident: LazyLock<Uid> = LazyLock::new(|| { -                Uid::new_unique(UidKind::Component) -            }); -        }; - -        let get_id = quote! { -            *#id_lazylock_ident -        }; - -        (id_lazylock, get_id)      };      let mod_ident = format_ident!( @@ -107,7 +78,6 @@ pub fn component_derive(input: TokenStream) -> TokenStream          mod #mod_ident {              use ::std::any::{Any, TypeId};              use ::std::sync::{LazyLock, Mutex}; -            use ::std::collections::HashMap;              use #ecs_path::component::Component;              use #ecs_path::component::{ @@ -119,7 +89,7 @@ pub fn component_derive(input: TokenStream) -> TokenStream              use super::*; -            #id_or_ids +            #id_var              impl #impl_generics Component for #item_ident #type_generics              #where_clause @@ -129,7 +99,7 @@ pub fn component_derive(input: TokenStream) -> TokenStream                  fn id() -> Uid                  { -                    #get_id +                    *#id_var_ident                  }                  fn name(&self) -> &'static str | 
