diff options
Diffstat (limited to 'engine-macros/src/lib.rs')
| -rw-r--r-- | engine-macros/src/lib.rs | 94 |
1 files changed, 33 insertions, 61 deletions
diff --git a/engine-macros/src/lib.rs b/engine-macros/src/lib.rs index ad6c15f..b87acf8 100644 --- a/engine-macros/src/lib.rs +++ b/engine-macros/src/lib.rs @@ -2,19 +2,7 @@ use proc_macro::TokenStream; use quote::{ToTokens, format_ident, quote}; -use syn::punctuated::Punctuated; -use syn::{ - Item, - LitStr, - Path as SynPath, - PredicateType, - Token, - TraitBound, - TypeParamBound, - WhereClause, - WherePredicate, - parse, -}; +use syn::{Item, LitStr, Path as SynPath, parse}; macro_rules! syn_path { ($first_segment: ident $(::$segment: ident)*) => { @@ -57,29 +45,6 @@ pub fn reflection_derive(input: TokenStream) -> TokenStream let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl(); - let mut where_clause = where_clause.cloned().unwrap_or_else(|| WhereClause { - where_token: <Token![where]>::default(), - predicates: Punctuated::new(), - }); - - where_clause - .predicates - .extend(input.fields.iter().map(|field| { - WherePredicate::Type(PredicateType { - lifetimes: None, - bounded_ty: field.ty.clone(), - colon_token: <Token![:]>::default(), - bounds: [TypeParamBound::Trait(TraitBound { - paren_token: None, - modifier: syn::TraitBoundModifier::None, - lifetimes: None, - path: engine_crate_path.join(syn_path!(reflection::With)), - })] - .into_iter() - .collect(), - }) - })); - let fields = input.fields.into_iter().enumerate().map(|(index, field)| { let field_ident = field.ident.unwrap_or_else(|| format_ident!("{index}")); @@ -98,10 +63,38 @@ pub fn reflection_derive(input: TokenStream) -> TokenStream byte_offset: std::mem::offset_of!(Self, #field_ident), type_id: std::any::TypeId::of::<#field_type>(), type_name: #field_type_name, - reflection: - #engine_crate_path::reflection::__private::get_type_reflection::< - #field_type - >() + get_reflection: #engine_crate_path::reflection::FnWithDebug::new(|| { + struct SpecializationTarget<Field>(std::marker::PhantomData<Field>); + + trait FieldHasReflection + { + fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection>; + } + + trait FieldDoesNotHaveReflection + { + fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection>; + } + + impl<Field> FieldDoesNotHaveReflection for &SpecializationTarget<Field> + { + fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection> + { + None + } + } + + impl<Field: #engine_crate_path::reflection::With> FieldHasReflection for SpecializationTarget<Field> + { + fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection> + { + Some(Field::reflection()) + } + } + + (&SpecializationTarget::<#field_type>(std::marker::PhantomData)) + .field_reflection() + }) } } }); @@ -151,24 +144,3 @@ fn find_engine_crate_path() -> Option<SynPath> Some(syn_path!(engine)) } - -trait SynPathExt -{ - fn join(&self, other: Self) -> Self; -} - -impl SynPathExt for SynPath -{ - fn join(&self, other: Self) -> Self - { - Self { - leading_colon: self.leading_colon.clone(), - segments: self - .segments - .iter() - .chain(&other.segments) - .cloned() - .collect(), - } - } -} |
