From 52e42bfdb3d56644da03e60969b1a2bedac8efc2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 12 Jun 2026 20:41:38 +0200 Subject: fix(engine): no field type refs for generic types deriving Reflection --- engine-macros/src/reflection/field.rs | 105 ++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 36 deletions(-) (limited to 'engine-macros/src/reflection/field.rs') diff --git a/engine-macros/src/reflection/field.rs b/engine-macros/src/reflection/field.rs index 7d4e301..95671c3 100644 --- a/engine-macros/src/reflection/field.rs +++ b/engine-macros/src/reflection/field.rs @@ -6,6 +6,7 @@ pub struct ReflectionFieldGenOptions<'a> { pub field_vis_override: Option, pub gen_get_byte_offset: &'a dyn Fn(&syn::Field) -> proc_macro2::TokenStream, + pub type_reflection_optional: bool, } pub fn generate( @@ -33,6 +34,12 @@ pub fn generate( let field_reflection_vis = generate_visibility(field_vis, &engine_crate_path); + let get_type_fn_body = if options.type_reflection_optional { + gen_get_optional_type_reflection(field_type, engine_crate_path) + } else { + gen_get_mandatory_type_reflection(field_type, engine_crate_path) + }; + quote! { #engine_crate_path::reflection::Field { name: #field_name, @@ -44,44 +51,70 @@ pub fn generate( std::any::type_name::<#field_type>() }), get_type: #engine_crate_path::reflection::FnWithDebug::new(|| { - struct SpecializationTarget(std::marker::PhantomData); - - trait FieldHasReflection - { - fn field_type_reflection(&self) - -> Option<&'static #engine_crate_path::reflection::Type>; - } - - trait FieldDoesNotHaveReflection - { - fn field_type_reflection(&self) - -> Option<&'static #engine_crate_path::reflection::Type>; - } - - impl FieldDoesNotHaveReflection for &SpecializationTarget - { - fn field_type_reflection(&self) - -> Option<&'static #engine_crate_path::reflection::Type> - { - None - } - } - - impl FieldHasReflection for SpecializationTarget - where - Field: #engine_crate_path::reflection::Reflection - { - fn field_type_reflection(&self) - -> Option<&'static #engine_crate_path::reflection::Type> - { - Some(Field::type_reflection()) - } - } - - (&SpecializationTarget::<#field_type>(std::marker::PhantomData)) - .field_type_reflection() + #get_type_fn_body }), visibility: #field_reflection_vis } } } + +fn gen_get_optional_type_reflection( + field_type: &syn::Type, + engine_crate_path: &syn::Path, +) -> proc_macro2::TokenStream +{ + quote! { + struct SpecializationTarget(std::marker::PhantomData); + + trait FieldHasReflection + { + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type>; + } + + trait FieldDoesNotHaveReflection + { + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type>; + } + + impl FieldDoesNotHaveReflection for &SpecializationTarget + { + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type> + { + None + } + } + + impl FieldHasReflection for SpecializationTarget + where + Field: #engine_crate_path::reflection::Reflection + { + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type> + { + Some(Field::type_reflection()) + } + } + + (&SpecializationTarget::<#field_type>(std::marker::PhantomData)) + .field_type_reflection() + } +} + +fn gen_get_mandatory_type_reflection( + field_type: &syn::Type, + engine_crate_path: &syn::Path, +) -> proc_macro2::TokenStream +{ + quote! { + fn field_type_reflection() + -> &'static #engine_crate_path::reflection::Type + { + T::type_reflection() + } + + Some(field_type_reflection::<#field_type>()) + } +} -- cgit v1.2.3-18-g5258