diff options
| author | HampusM <hampus@hampusmat.com> | 2026-11-30 16:13:07 +0100 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-11-30 16:13:07 +0100 |
| commit | a400a0f378b1c4d911fd21f02d1928271f5930d3 (patch) | |
| tree | 41b347be9df86deec918e0bd53744971686e0d1f /engine-reflection | |
| parent | 46e021944a6047cc0715ff7034c07a03d073016c (diff) | |
feat(engine): add type IDs to type reflection data
Diffstat (limited to 'engine-reflection')
| -rw-r--r-- | engine-reflection/src/lib.rs | 24 |
1 files changed, 24 insertions, 0 deletions
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 @@ -75,6 +75,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<Box<dyn Any>> { match self { @@ -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<DefaultValueFn>, 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<T: Reflection, const LEN: usize> Reflection for [T; LEN] { const TYPE_REFLECTION: &Type = &Type::Array(Array { + type_id: TypeId::of::<Self>(), item_type: T::TYPE_REFLECTION, item_layout: Layout::new::<T>(), get_item_type_name: FnWithDebug::new(|| type_name::<T>()), @@ -488,6 +509,7 @@ unsafe impl<T: Reflection, const LEN: usize> Reflection for [T; LEN] unsafe impl<T: Reflection> Reflection for &'static [T] { const TYPE_REFLECTION: &Type = &Type::Slice(Slice { + type_id: TypeId::of::<Self>(), item_type: T::TYPE_REFLECTION, item_layout: Layout::new::<T>(), get_item_type_name: FnWithDebug::new(|| type_name::<T>()), @@ -511,6 +533,7 @@ unsafe impl<T: Reflection> Reflection for &'static [T] unsafe impl<T: Reflection> Reflection for &'static T { const TYPE_REFLECTION: &Type = &Type::Reference(Reference { + type_id: TypeId::of::<Self>(), ty: T::TYPE_REFLECTION, cast_dyn_any: |ptr| ptr.cast::<Self>(), try_deref: |target| { @@ -524,6 +547,7 @@ unsafe impl<T: Reflection> Reflection for &'static T unsafe impl<T: Reflection> Reflection for Option<T> { const TYPE_REFLECTION: &Type = &Type::Enum(Enum { + type_id: TypeId::of::<Self>(), variants: &[ EnumVariant { name: "Some", |
