From a400a0f378b1c4d911fd21f02d1928271f5930d3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 30 Nov 2026 16:13:07 +0100 Subject: feat(engine): add type IDs to type reflection data --- engine-reflection/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'engine-reflection') diff --git a/engine-reflection/src/lib.rs b/engine-reflection/src/lib.rs index 3ec26cb..2bb749a 100644 --- a/engine-reflection/src/lib.rs +++ b/engine-reflection/src/lib.rs @@ -73,6 +73,20 @@ impl Type } } + #[inline] + #[must_use] + pub fn ty_id(&self) -> TypeId + { + match self { + Self::Struct(struct_type) => struct_type.type_id, + Self::Enum(enum_type) => enum_type.type_id, + Self::Literal(literal_type) => literal_type.type_id, + Self::Array(array_type) => array_type.type_id, + Self::Reference(ref_type) => ref_type.type_id, + Self::Slice(slice_type) => slice_type.type_id, + } + } + #[inline] #[must_use] pub fn default_value(&self) -> Option> @@ -115,6 +129,7 @@ impl Type #[derive(Debug, Clone)] pub struct Struct { + pub type_id: TypeId, pub fields: &'static [Field], pub get_default_value: fn() -> Option, pub cast_dyn_any: CastDynAnyFn, @@ -144,6 +159,8 @@ impl Struct #[derive(Debug, Clone)] pub struct Enum { + pub type_id: TypeId, + /// Enum variants in the same order as in the enum definition. pub variants: &'static [EnumVariant], @@ -305,6 +322,7 @@ impl Field #[derive(Debug, Clone)] pub struct Array { + pub type_id: TypeId, pub item_type: &'static Type, pub item_layout: Layout, pub get_item_type_name: FnWithDebug<&'static str>, @@ -327,6 +345,7 @@ impl Array #[derive(Debug, Clone)] pub struct Slice { + pub type_id: TypeId, pub item_type: &'static Type, pub item_layout: Layout, pub get_item_type_name: FnWithDebug<&'static str>, @@ -375,6 +394,7 @@ impl Literal #[derive(Debug)] pub struct Reference { + pub type_id: TypeId, pub ty: &'static Type, pub cast_dyn_any: CastDynAnyFn, pub try_deref: fn(target: &dyn Any) -> Option<&dyn Any>, @@ -459,6 +479,7 @@ impl_reflection_for_literals!( unsafe impl Reflection for [T; LEN] { const TYPE_REFLECTION: &Type = &Type::Array(Array { + type_id: TypeId::of::(), item_type: T::TYPE_REFLECTION, item_layout: Layout::new::(), get_item_type_name: FnWithDebug::new(|| type_name::()), @@ -488,6 +509,7 @@ unsafe impl Reflection for [T; LEN] unsafe impl Reflection for &'static [T] { const TYPE_REFLECTION: &Type = &Type::Slice(Slice { + type_id: TypeId::of::(), item_type: T::TYPE_REFLECTION, item_layout: Layout::new::(), get_item_type_name: FnWithDebug::new(|| type_name::()), @@ -511,6 +533,7 @@ unsafe impl Reflection for &'static [T] unsafe impl Reflection for &'static T { const TYPE_REFLECTION: &Type = &Type::Reference(Reference { + type_id: TypeId::of::(), ty: T::TYPE_REFLECTION, cast_dyn_any: |ptr| ptr.cast::(), try_deref: |target| { @@ -524,6 +547,7 @@ unsafe impl Reflection for &'static T unsafe impl Reflection for Option { const TYPE_REFLECTION: &Type = &Type::Enum(Enum { + type_id: TypeId::of::(), variants: &[ EnumVariant { name: "Some", -- cgit v1.2.3-18-g5258