/// Try creating a ComponentRegistry from derivation
Try creating a ComponentRegistry from derivation
Try creating a ComponentRegistry from derivation
fn impl_component_registry(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let mut gen = None;
    let data = &ast.data;
    if let Data::Struct(data) = data {
        if let Fields::Named(fields) = &data.fields {
            if let Some(field) = fields.named.first() {
                let component_type = &field.ty;
                let component_type = quote!(#component_type);
                let component = hash_map_value_type!(component_type);
                let data = &component.data;
                if let Data::Struct(data) = data {
                    if let Fields::Named(fields) = &data.fields {
                        if let Some(field) = fields.named.first() {
                            let component_type = &field.ty;
                            gen = Some(quote! {
                                    impl ComponentRegistry<#component_type> for #name {
                                        fn new() -> Self {
                                            Self {
                                                components: HashMap::new(),
                                            }
                                        }
                                        fn add(&mut self, entity: u32, component: #component_type) {
                                            self.components.insert(entity, component);
                                        }
                                        fn add_many(&mut self, entities: &Vec<u32>, component: #component_type) {
                                            for entity in entities.iter() {
                                                self.components.insert(*entity, component);
                                            }
                                        }
                                        fn remove(&mut self, entity: u32) {
                                            self.components.remove(&entity);
                                        }
                                        fn get(&self, entity: u32) -> Option<&#component_type> {
                                            self.components.get(&entity)
                                        }
                                    }
                            });
                        }
                    }
                }
            }
        }
    }
    return match gen {
        Some(gen) => gen.into(),
        None => panic!("Cannot derive into a ComponentRegistry"),
    };
}
fn impl_component_registry ( ast : & syn :: DeriveInput ) -> TokenStream { let name = & ast . ident ; let mut gen = None ; let data = & ast . data ; if let Data :: Struct ( data ) = data { if let Fields :: Named ( fields ) = & data . fields { if let Some ( field ) = fields . named . first ( ) { let component_type = & field . ty ; let component_type = quote ! ( component_type ) ; let component = hash_map_value_type ! ( component_type ) ; let data = & component . data ; if let Data :: Struct ( data ) = data { if let Fields :: Named ( fields ) = & data . fields { if let Some ( field ) = fields . named . first ( ) { let component_type = & field . ty ; gen = Some ( quote ! { impl ComponentRegistry component_type for name { fn new ( ) Self { Self { components HashMap new ( ) } } fn add ( mut self entity u32 component component_type ) { self components insert ( entity component ) } fn add_many ( mut self entities Vec u32 component component_type ) { for entity in entities iter ( ) { self components insert ( entity component ) } } fn remove ( mut self entity u32 ) { self components remove ( entity ) } fn get ( self entity u32 ) Option component_type { self components get ( entity ) } } } ) ; } } } } } } return match gen { Some ( gen ) => gen . into ( ) , None => panic ! ( " " ) , } ; }
Try creating a ComponentRegistry from derivation
Try creating a ComponentRegistry from derivation
ast
&syn::DeriveInput