bevy_reflect: get_represented_kind_info APIs for reflected kinds (#14380)

# Objective

Fixes #14378

---------

Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
Co-authored-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
Torstein Grindvik 2024-10-15 04:08:31 +02:00 committed by GitHub
parent b78a060af2
commit 9f5f5d3d41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 0 deletions

View file

@ -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.

View file

@ -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).

View file

@ -1864,6 +1864,41 @@ mod tests {
assert!(info.is::<MyValue>());
}
#[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)]

View file

@ -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.

View file

@ -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<Box<dyn PartialReflect>>;
/// 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.

View file

@ -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.

View file

@ -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.

View file

@ -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.