From 9f5f5d3d41b85cedb4a8fb499558bd40bbdba3c5 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com> Date: Tue, 15 Oct 2024 04:08:31 +0200 Subject: [PATCH] bevy_reflect: get_represented_kind_info APIs for reflected kinds (#14380) # Objective Fixes #14378 --------- Signed-off-by: Torstein Grindvik Co-authored-by: Torstein Grindvik Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Co-authored-by: Alice Cecile --- crates/bevy_reflect/src/array.rs | 5 +++ crates/bevy_reflect/src/enums/enum_trait.rs | 7 +++++ crates/bevy_reflect/src/lib.rs | 35 +++++++++++++++++++++ crates/bevy_reflect/src/list.rs | 5 +++ crates/bevy_reflect/src/map.rs | 5 +++ crates/bevy_reflect/src/struct_trait.rs | 5 +++ crates/bevy_reflect/src/tuple.rs | 5 +++ crates/bevy_reflect/src/tuple_struct.rs | 5 +++ 8 files changed, 72 insertions(+) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index bdda829d3e..481b29648b 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -74,6 +74,11 @@ pub trait Array: PartialReflect { values: self.iter().map(PartialReflect::clone_value).collect(), } } + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_array_info(&self) -> Option<&'static ArrayInfo> { + self.get_represented_type_info()?.as_array().ok() + } } /// A container for compile-time array info. diff --git a/crates/bevy_reflect/src/enums/enum_trait.rs b/crates/bevy_reflect/src/enums/enum_trait.rs index 431b4c3ff3..d2325439c3 100644 --- a/crates/bevy_reflect/src/enums/enum_trait.rs +++ b/crates/bevy_reflect/src/enums/enum_trait.rs @@ -133,6 +133,13 @@ pub trait Enum: PartialReflect { fn variant_path(&self) -> String { format!("{}::{}", self.reflect_type_path(), self.variant_name()) } + + /// Will return `None` if [`TypeInfo`] is not available. + /// + /// [`TypeInfo`]: crate::TypeInfo + fn get_represented_enum_info(&self) -> Option<&'static EnumInfo> { + self.get_represented_type_info()?.as_enum().ok() + } } /// A container for compile-time enum info, used by [`TypeInfo`](crate::TypeInfo). diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 6931309a54..92fe96edc4 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1864,6 +1864,41 @@ mod tests { assert!(info.is::()); } + #[test] + fn get_represented_kind_info() { + #[derive(Reflect)] + struct SomeStruct; + + #[derive(Reflect)] + struct SomeTupleStruct(f32); + + #[derive(Reflect)] + enum SomeEnum { + Foo, + Bar, + } + + let dyn_struct: &dyn Struct = &SomeStruct; + let _: &StructInfo = dyn_struct.get_represented_struct_info().unwrap(); + + let dyn_map: &dyn Map = &HashMap::<(), ()>::default(); + let _: &MapInfo = dyn_map.get_represented_map_info().unwrap(); + + let dyn_array: &dyn Array = &[1, 2, 3]; + let _: &ArrayInfo = dyn_array.get_represented_array_info().unwrap(); + + let dyn_list: &dyn List = &vec![1, 2, 3]; + let _: &ListInfo = dyn_list.get_represented_list_info().unwrap(); + + let dyn_tuple_struct: &dyn TupleStruct = &SomeTupleStruct(5.0); + let _: &TupleStructInfo = dyn_tuple_struct + .get_represented_tuple_struct_info() + .unwrap(); + + let dyn_enum: &dyn Enum = &SomeEnum::Foo; + let _: &EnumInfo = dyn_enum.get_represented_enum_info().unwrap(); + } + #[test] fn should_permit_higher_ranked_lifetimes() { #[derive(Reflect)] diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index 1eea9eaff2..49a479154f 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -109,6 +109,11 @@ pub trait List: PartialReflect { values: self.iter().map(PartialReflect::clone_value).collect(), } } + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_list_info(&self) -> Option<&'static ListInfo> { + self.get_represented_type_info()?.as_list().ok() + } } /// A container for compile-time list info. diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index ffb56a7b02..03a4a325ef 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -99,6 +99,11 @@ pub trait Map: PartialReflect { /// If the map did not have this key present, `None` is returned. /// If the map did have this key present, the removed value is returned. fn remove(&mut self, key: &dyn PartialReflect) -> Option>; + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_map_info(&self) -> Option<&'static MapInfo> { + self.get_represented_type_info()?.as_map().ok() + } } /// A container for compile-time map info. diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 0b85ad35bb..93fa1fac67 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -73,6 +73,11 @@ pub trait Struct: PartialReflect { /// Clones the struct into a [`DynamicStruct`]. fn clone_dynamic(&self) -> DynamicStruct; + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_struct_info(&self) -> Option<&'static StructInfo> { + self.get_represented_type_info()?.as_struct().ok() + } } /// A container for compile-time named struct info. diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 5eb3d3817e..3807ac2fce 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -56,6 +56,11 @@ pub trait Tuple: PartialReflect { /// Clones the struct into a [`DynamicTuple`]. fn clone_dynamic(&self) -> DynamicTuple; + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_tuple_info(&self) -> Option<&'static TupleInfo> { + self.get_represented_type_info()?.as_tuple().ok() + } } /// An iterator over the field values of a tuple. diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index a38743e8f0..79f3d46ac7 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -57,6 +57,11 @@ pub trait TupleStruct: PartialReflect { /// Clones the struct into a [`DynamicTupleStruct`]. fn clone_dynamic(&self) -> DynamicTupleStruct; + + /// Will return `None` if [`TypeInfo`] is not available. + fn get_represented_tuple_struct_info(&self) -> Option<&'static TupleStructInfo> { + self.get_represented_type_info()?.as_tuple_struct().ok() + } } /// A container for compile-time tuple struct info.