From 23c4199cac2ce8c0c91ac1e3d9b46fcc1ff72d7a Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 3 Jul 2026 21:13:09 +0200 Subject: feat(engine): make enum variant field offsets in reflection not absolute --- engine-macros/src/reflection/enum_impl.rs | 8 -------- engine-reflection/src/lib.rs | 26 +++++++++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/engine-macros/src/reflection/enum_impl.rs b/engine-macros/src/reflection/enum_impl.rs index 41aa673..ea83681 100644 --- a/engine-macros/src/reflection/enum_impl.rs +++ b/engine-macros/src/reflection/enum_impl.rs @@ -227,10 +227,6 @@ fn generate_variants<'a>( gen_get_byte_offset: &|field| { if let Some(field_ident) = &field.ident { quote! { - std::mem::offset_of!( - #mod_name::Equivalent #generic_args, - payload.#variant_ident - ) + std::mem::offset_of!( #mod_name::#variant_fields_struct_ident #generic_args, @@ -274,10 +270,6 @@ fn generate_variants<'a>( ); quote! { - std::mem::offset_of!( - #mod_name::Equivalent #generic_args, - payload.#variant_ident - ) + std::mem::offset_of!( #mod_name::#variant_fields_struct_ident #generic_args, diff --git a/engine-reflection/src/lib.rs b/engine-reflection/src/lib.rs index 600c511..9f347fa 100644 --- a/engine-reflection/src/lib.rs +++ b/engine-reflection/src/lib.rs @@ -91,14 +91,15 @@ impl Type } #[inline] - pub fn cast_dyn_any(&self, ptr: NonNull) -> Option> { + pub fn cast_dyn_any(&self, ptr: NonNull) -> Option> + { match self { Self::Struct(struct_type) => Some((struct_type.cast_dyn_any)(ptr)), Self::Enum(enum_type) => Some((enum_type.cast_dyn_any)(ptr)), Self::Literal(literal_type) => Some((literal_type.cast_dyn_any)(ptr)), Self::Array(array_type) => Some((array_type.cast_dyn_any)(ptr)), Self::Reference(ref_type) => Some((ref_type.cast_dyn_any)(ptr)), - Self::Slice(_) => None + Self::Slice(_) => None, } } } @@ -108,7 +109,7 @@ pub struct Struct { pub fields: &'static [Field], pub get_default_value: fn() -> Option, - pub cast_dyn_any: CastDynAnyFn + pub cast_dyn_any: CastDynAnyFn, } impl Struct @@ -140,7 +141,7 @@ pub struct Enum pub tagged_union: Option, pub get_default_value: fn() -> Option, - pub cast_dyn_any: CastDynAnyFn + pub cast_dyn_any: CastDynAnyFn, } impl Enum @@ -218,8 +219,11 @@ pub struct Field pub index: usize, pub layout: Layout, - /// Byte offset to the field relative to the start of the type containing it. If the - /// type containing the field is a enum, this offset will include the enum tag + /// Byte offset to the field relative to the start of the type containing it. + /// + /// If the type containing the field is a enum variant, this offset is relative to + /// the payload field of the enum's equivalent tagged union, therefore this offset + /// will **NOT** include the enum tag pub byte_offset: usize, pub type_id: TypeId, @@ -248,7 +252,7 @@ pub struct Array pub item_layout: Layout, pub get_item_type_name: FnWithDebug<&'static str>, pub length: usize, - pub cast_dyn_any: CastDynAnyFn + pub cast_dyn_any: CastDynAnyFn, } impl Array @@ -284,7 +288,7 @@ pub struct Literal pub ty: LiteralType, pub type_name: fn() -> &'static str, pub get_default_value: fn() -> Option, - pub cast_dyn_any: CastDynAnyFn + pub cast_dyn_any: CastDynAnyFn, } impl Literal @@ -306,7 +310,7 @@ impl Literal pub struct Reference { pub ty: &'static Type, - pub cast_dyn_any: CastDynAnyFn + pub cast_dyn_any: CastDynAnyFn, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -392,7 +396,7 @@ unsafe impl Reflection for [T; LEN] item_layout: Layout::new::(), get_item_type_name: FnWithDebug::new(|| type_name::()), length: LEN, - cast_dyn_any: |ptr| ptr.cast::() + cast_dyn_any: |ptr| ptr.cast::(), }); } @@ -409,7 +413,7 @@ unsafe impl Reflection for &'static T { const TYPE_REFLECTION: &Type = &Type::Reference(Reference { ty: T::TYPE_REFLECTION, - cast_dyn_any: |ptr| ptr.cast::() + cast_dyn_any: |ptr| ptr.cast::(), }); } -- cgit v1.2.3-18-g5258