From 942df064017258a92eee1a14cd613c6aec983dc8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 6 Apr 2026 18:50:04 +0200 Subject: feat(engine): make reflection of reflected struct fields optional --- engine-macros/src/lib.rs | 94 +++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 61 deletions(-) (limited to 'engine-macros') 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: ::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: ::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(std::marker::PhantomData); + + 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 FieldDoesNotHaveReflection for &SpecializationTarget + { + fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection> + { + None + } + } + + impl FieldHasReflection for SpecializationTarget + { + 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 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(), - } - } -} -- cgit v1.2.3-18-g5258