diff options
| author | HampusM <hampus@hampusmat.com> | 2026-07-07 21:43:55 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-07-07 21:43:55 +0200 |
| commit | 28686c9359da2c2c46d12ae8aaada14ece17246f (patch) | |
| tree | a2a260384eb53b214a5837284a594bd3c12f56ad | |
| parent | 0820dde7d6099f87f9eda6240b64306900e00855 (diff) | |
feat(engine): change enum reflection get_variant_reflection fn to get_variant_index
| -rw-r--r-- | engine-macros/src/reflection/enum_impl.rs | 19 | ||||
| -rw-r--r-- | engine-reflection/src/lib.rs | 9 |
2 files changed, 21 insertions, 7 deletions
diff --git a/engine-macros/src/reflection/enum_impl.rs b/engine-macros/src/reflection/enum_impl.rs index 1cc8f0a..4e27c2b 100644 --- a/engine-macros/src/reflection/enum_impl.rs +++ b/engine-macros/src/reflection/enum_impl.rs @@ -110,15 +110,26 @@ fn generate_impls( #get_default_value_fn }, cast_dyn_any: |ptr| ptr.cast::<Self>(), - get_variant_reflection: |target| { + get_variant_index: |target| { use std::any::Any; + use #engine_crate_path::reflection::Reflection; use #engine_crate_path::reflection::EnumReflectionExt; let target = target.downcast_ref::<Self>()?; - Some(<Self as EnumReflectionExt>::get_variant_reflection( - target - )) + let enum_reflection = unsafe { + <Self as Reflection>::TYPE_REFLECTION + .as_enum() + .unwrap_unchecked() + }; + + Some(enum_reflection.variants + .element_offset( + match target { + #(#variant_lookup_match_arms),* + } + ) + .unwrap()) } } ) diff --git a/engine-reflection/src/lib.rs b/engine-reflection/src/lib.rs index a76abce..a31fef0 100644 --- a/engine-reflection/src/lib.rs +++ b/engine-reflection/src/lib.rs @@ -138,7 +138,7 @@ pub struct Enum pub get_default_value: fn() -> Option<DefaultValueFn>, pub cast_dyn_any: CastDynAnyFn, - pub get_variant_reflection: fn(&dyn Any) -> Option<&'static EnumVariant>, + pub get_variant_index: fn(&dyn Any) -> Option<usize>, } impl Enum @@ -490,10 +490,13 @@ unsafe impl<T: Reflection> Reflection for Option<T> is_unit_only: false, get_default_value: || Some(|| Box::new(Option::<T>::None)), cast_dyn_any: |ptr| ptr.cast::<Self>(), - get_variant_reflection: |target| { + get_variant_index: |target| { let target = target.downcast_ref::<Self>()?; - Some(target.get_variant_reflection()) + Some(match target { + Some(_) => 0, + None => 1, + }) }, }); } |
