mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
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:
parent
b78a060af2
commit
9f5f5d3d41
8 changed files with 72 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue