From 8b9d88f4d0f7a3d99912de1c8fc20b5e87e9236e Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 6 Jun 2023 12:23:58 -0500 Subject: [PATCH] Reflect now requires DynamicTypePath. Remove Reflect::get_type_path() (#8764) Followup to #7184 This makes `Reflect: DynamicTypePath` which allows us to remove `Reflect::get_type_path`, reducing unnecessary codegen and simplifying `Reflect` implementations. --- .../bevy_reflect_derive/src/impls/enums.rs | 5 --- .../bevy_reflect_derive/src/impls/structs.rs | 5 --- .../src/impls/tuple_structs.rs | 5 --- .../bevy_reflect_derive/src/impls/values.rs | 5 --- crates/bevy_reflect/src/array.rs | 12 ++---- crates/bevy_reflect/src/enums/dynamic_enum.rs | 9 +---- crates/bevy_reflect/src/impls/smallvec.rs | 11 ++---- crates/bevy_reflect/src/impls/std.rs | 38 +------------------ crates/bevy_reflect/src/list.rs | 8 +--- crates/bevy_reflect/src/map.rs | 9 +---- crates/bevy_reflect/src/reflect.rs | 10 +---- crates/bevy_reflect/src/struct_trait.rs | 8 +--- crates/bevy_reflect/src/tuple.rs | 16 ++------ crates/bevy_reflect/src/tuple_struct.rs | 8 +--- crates/bevy_reflect/src/type_info.rs | 8 +++- crates/bevy_reflect/src/type_path.rs | 4 -- crates/bevy_reflect/src/utility.rs | 18 ++++++--- 17 files changed, 36 insertions(+), 143 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs index 365061df4f..1a41f8e115 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs @@ -199,11 +199,6 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream { #FQOption::Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath { - self - } - #[inline] fn into_any(self: #FQBox) -> #FQBox { self diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs index 9c5d30ebad..17180c16b1 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs @@ -174,11 +174,6 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream { #FQOption::Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath { - self - } - #[inline] fn into_any(self: #FQBox) -> #FQBox { self diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs index db4b839baa..0a5cbd8343 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs @@ -144,11 +144,6 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream { #FQOption::Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath { - self - } - #[inline] fn into_any(self: #FQBox) -> #FQBox { self diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs index b61b1ca9ba..5fc4fa8dee 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs @@ -56,11 +56,6 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream { #FQOption::Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath { - self - } - #[inline] fn into_any(self: #FQBox) -> #FQBox { self diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 7a4375f83a..c1acb98356 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -1,9 +1,8 @@ -use bevy_reflect_derive::impl_type_path; - use crate::{ - self as bevy_reflect, utility::reflect_hasher, DynamicTypePath, Reflect, ReflectMut, - ReflectOwned, ReflectRef, TypeInfo, + self as bevy_reflect, utility::reflect_hasher, Reflect, ReflectMut, ReflectOwned, ReflectRef, + TypeInfo, }; +use bevy_reflect_derive::impl_type_path; use std::{ any::{Any, TypeId}, fmt::Debug, @@ -226,11 +225,6 @@ impl Reflect for DynamicArray { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/enums/dynamic_enum.rs b/crates/bevy_reflect/src/enums/dynamic_enum.rs index 6edc256c68..32b752836d 100644 --- a/crates/bevy_reflect/src/enums/dynamic_enum.rs +++ b/crates/bevy_reflect/src/enums/dynamic_enum.rs @@ -2,8 +2,8 @@ use bevy_reflect_derive::impl_type_path; use crate::{ self as bevy_reflect, enum_debug, enum_hash, enum_partial_eq, DynamicStruct, DynamicTuple, - DynamicTypePath, Enum, Reflect, ReflectMut, ReflectOwned, ReflectRef, Struct, Tuple, TypeInfo, - VariantFieldIter, VariantType, + Enum, Reflect, ReflectMut, ReflectOwned, ReflectRef, Struct, Tuple, TypeInfo, VariantFieldIter, + VariantType, }; use std::any::Any; use std::fmt::Formatter; @@ -300,11 +300,6 @@ impl Reflect for DynamicEnum { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/impls/smallvec.rs b/crates/bevy_reflect/src/impls/smallvec.rs index eb6a4b1c02..1f7bce2b8b 100644 --- a/crates/bevy_reflect/src/impls/smallvec.rs +++ b/crates/bevy_reflect/src/impls/smallvec.rs @@ -4,9 +4,9 @@ use std::any::Any; use crate::utility::GenericTypeInfoCell; use crate::{ - self as bevy_reflect, DynamicTypePath, FromReflect, FromType, GetTypeRegistration, List, - ListInfo, ListIter, Reflect, ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, - TypePath, TypeRegistration, Typed, + self as bevy_reflect, FromReflect, FromType, GetTypeRegistration, List, ListInfo, ListIter, + Reflect, ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypePath, + TypeRegistration, Typed, }; impl List for SmallVec @@ -88,11 +88,6 @@ where Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 806aff9fa8..9f21e2e3f0 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -2,8 +2,8 @@ use crate::std_traits::ReflectDefault; use crate::{self as bevy_reflect, ReflectFromPtr, ReflectFromReflect, ReflectOwned}; use crate::{ impl_type_path, map_apply, map_partial_eq, Array, ArrayInfo, ArrayIter, DynamicEnum, - DynamicMap, DynamicTypePath, Enum, EnumInfo, FromReflect, FromType, GetTypeRegistration, List, - ListInfo, ListIter, Map, MapInfo, MapIter, Reflect, ReflectDeserialize, ReflectMut, ReflectRef, + DynamicMap, Enum, EnumInfo, FromReflect, FromType, GetTypeRegistration, List, ListInfo, + ListIter, Map, MapInfo, MapIter, Reflect, ReflectDeserialize, ReflectMut, ReflectRef, ReflectSerialize, TupleVariantInfo, TypeInfo, TypePath, TypeRegistration, Typed, UnitVariantInfo, UnnamedField, ValueInfo, VariantFieldIter, VariantInfo, VariantType, }; @@ -332,11 +332,6 @@ macro_rules! impl_reflect_for_veclike { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } @@ -555,10 +550,6 @@ macro_rules! impl_reflect_for_hashmap { Some(::type_info()) } - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } @@ -721,11 +712,6 @@ impl Reflect for [T; N] { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self @@ -943,11 +929,6 @@ impl Reflect for Option { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self @@ -1115,11 +1096,6 @@ impl Reflect for Cow<'static, str> { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } @@ -1252,11 +1228,6 @@ impl Reflect for &'static Path { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } @@ -1366,11 +1337,6 @@ impl Reflect for Cow<'static, Path> { std::any::type_name::() } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { Some(::type_info()) } diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index b040464ec3..94c269c520 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -6,8 +6,7 @@ use bevy_reflect_derive::impl_type_path; use crate::utility::reflect_hasher; use crate::{ - self as bevy_reflect, DynamicTypePath, FromReflect, Reflect, ReflectMut, ReflectOwned, - ReflectRef, TypeInfo, + self as bevy_reflect, FromReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, }; /// A trait used to power [list-like] operations via [reflection]. @@ -276,11 +275,6 @@ impl Reflect for DynamicList { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index 3b9c0c751b..e31f4280c2 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -5,9 +5,7 @@ use std::hash::Hash; use bevy_reflect_derive::impl_type_path; use bevy_utils::{Entry, HashMap}; -use crate::{ - self as bevy_reflect, DynamicTypePath, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, -}; +use crate::{self as bevy_reflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo}; /// A trait used to power [map-like] operations via [reflection]. /// @@ -310,11 +308,6 @@ impl Reflect for DynamicMap { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } diff --git a/crates/bevy_reflect/src/reflect.rs b/crates/bevy_reflect/src/reflect.rs index 53bf3aca1a..9c33ff6753 100644 --- a/crates/bevy_reflect/src/reflect.rs +++ b/crates/bevy_reflect/src/reflect.rs @@ -72,7 +72,7 @@ pub enum ReflectOwned { /// [`bevy_reflect`]: crate /// [derive macro]: bevy_reflect_derive::Reflect /// [crate-level documentation]: crate -pub trait Reflect: Any + Send + Sync { +pub trait Reflect: DynamicTypePath + Any + Send + Sync { /// Returns the [type name][std::any::type_name] of the underlying type. fn type_name(&self) -> &str; @@ -93,14 +93,6 @@ pub trait Reflect: Any + Send + Sync { /// [`TypeRegistry::get_type_info`]: crate::TypeRegistry::get_type_info fn get_represented_type_info(&self) -> Option<&'static TypeInfo>; - /// Returns the [`TypePath`] implementation for the underlying type. - /// - /// Methods on [`DynamicTypePath`] suffer the same performance concerns as [`get_represented_type_info`]. - /// - /// [`TypePath`]: crate::TypePath - /// [`get_represented_type_info`]: Reflect::get_represented_type_info - fn get_type_path(&self) -> &dyn DynamicTypePath; - /// Returns the value as a [`Box`][std::any::Any]. fn into_any(self: Box) -> Box; diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index fce88b78dd..36e3aaa56a 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -1,6 +1,5 @@ use crate::{ - self as bevy_reflect, DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, - ReflectRef, TypeInfo, + self as bevy_reflect, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, }; use bevy_reflect_derive::impl_type_path; use bevy_utils::{Entry, HashMap}; @@ -405,11 +404,6 @@ impl Reflect for DynamicStruct { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 2c7c6afaaa..0480d3b309 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -1,9 +1,9 @@ use bevy_reflect_derive::impl_type_path; use crate::{ - self as bevy_reflect, utility::GenericTypePathCell, DynamicTypePath, FromReflect, - GetTypeRegistration, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypePath, - TypeRegistration, Typed, UnnamedField, + self as bevy_reflect, utility::GenericTypePathCell, FromReflect, GetTypeRegistration, Reflect, + ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypePath, TypeRegistration, Typed, + UnnamedField, }; use std::any::{Any, TypeId}; use std::borrow::Cow; @@ -321,11 +321,6 @@ impl Reflect for DynamicTuple { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self @@ -538,11 +533,6 @@ macro_rules! impl_reflect_tuple { Some(::type_info()) } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - fn into_any(self: Box) -> Box { self } diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index b2703b65aa..ae455be391 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -1,8 +1,7 @@ use bevy_reflect_derive::impl_type_path; use crate::{ - self as bevy_reflect, DynamicTypePath, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, - UnnamedField, + self as bevy_reflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, UnnamedField, }; use std::any::{Any, TypeId}; use std::fmt::{Debug, Formatter}; @@ -308,11 +307,6 @@ impl Reflect for DynamicTupleStruct { self.represented_type } - #[inline] - fn get_type_path(&self) -> &dyn DynamicTypePath { - self - } - #[inline] fn into_any(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/type_info.rs b/crates/bevy_reflect/src/type_info.rs index 7a4099815a..6db1f26b49 100644 --- a/crates/bevy_reflect/src/type_info.rs +++ b/crates/bevy_reflect/src/type_info.rs @@ -24,7 +24,7 @@ use std::fmt::Debug; /// /// ``` /// # use std::any::Any; -/// # use bevy_reflect::{DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, TypeInfo, ValueInfo}; +/// # use bevy_reflect::{DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, TypeInfo, TypePath, ValueInfo}; /// # use bevy_reflect::utility::NonGenericTypeInfoCell; /// use bevy_reflect::Typed; /// @@ -51,7 +51,6 @@ use std::fmt::Debug; /// # impl Reflect for MyStruct { /// # fn type_name(&self) -> &str { todo!() } /// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } -/// # fn get_type_path(&self) -> &dyn DynamicTypePath { todo!() } /// # fn into_any(self: Box) -> Box { todo!() } /// # fn as_any(&self) -> &dyn Any { todo!() } /// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() } @@ -65,6 +64,11 @@ use std::fmt::Debug; /// # fn reflect_owned(self: Box) -> ReflectOwned { todo!() } /// # fn clone_value(&self) -> Box { todo!() } /// # } +/// # +/// # impl TypePath for MyStruct { +/// # fn type_path() -> &'static str { todo!() } +/// # fn short_type_path() -> &'static str { todo!() } +/// # } /// ``` /// /// [utility]: crate::utility diff --git a/crates/bevy_reflect/src/type_path.rs b/crates/bevy_reflect/src/type_path.rs index 387fb11930..8238c998cb 100644 --- a/crates/bevy_reflect/src/type_path.rs +++ b/crates/bevy_reflect/src/type_path.rs @@ -123,10 +123,6 @@ pub trait TypePath: 'static { } /// Dynamic dispatch for [`TypePath`]. -/// -/// Retrieved using [`Reflect::get_type_path`]. -/// -/// [`Reflect::get_type_path`]: crate::Reflect::get_type_path pub trait DynamicTypePath { /// See [`TypePath::type_path`]. fn reflect_type_path(&self) -> &str; diff --git a/crates/bevy_reflect/src/utility.rs b/crates/bevy_reflect/src/utility.rs index 6344072b08..0ffc09bb8c 100644 --- a/crates/bevy_reflect/src/utility.rs +++ b/crates/bevy_reflect/src/utility.rs @@ -50,7 +50,7 @@ mod sealed { /// /// ``` /// # use std::any::Any; -/// # use bevy_reflect::{DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, Typed, TypeInfo}; +/// # use bevy_reflect::{DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, Typed, TypeInfo, TypePath}; /// use bevy_reflect::utility::NonGenericTypeInfoCell; /// /// struct Foo { @@ -70,7 +70,6 @@ mod sealed { /// # /// # impl Reflect for Foo { /// # fn type_name(&self) -> &str { todo!() } -/// # fn get_type_path(&self) -> &dyn DynamicTypePath { todo!() } /// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_any(self: Box) -> Box { todo!() } /// # fn as_any(&self) -> &dyn Any { todo!() } @@ -85,6 +84,11 @@ mod sealed { /// # fn reflect_owned(self: Box) -> ReflectOwned { todo!() } /// # fn clone_value(&self) -> Box { todo!() } /// # } + +/// # impl TypePath for Foo { +/// # fn type_path() -> &'static str { todo!() } +/// # fn short_type_path() -> &'static str { todo!() } +/// # } /// ``` /// /// [`TypePath`]: crate::TypePath @@ -124,7 +128,7 @@ impl NonGenericTypeCell { /// /// ``` /// # use std::any::Any; -/// # use bevy_reflect::{DynamicTypePath, Reflect, ReflectMut, ReflectOwned, ReflectRef, TupleStructInfo, Typed, TypeInfo, UnnamedField}; +/// # use bevy_reflect::{DynamicTypePath, Reflect, ReflectMut, ReflectOwned, ReflectRef, TupleStructInfo, Typed, TypeInfo, TypePath, UnnamedField}; /// use bevy_reflect::utility::GenericTypeInfoCell; /// /// struct Foo(T); @@ -142,7 +146,6 @@ impl NonGenericTypeCell { /// # /// # impl Reflect for Foo { /// # fn type_name(&self) -> &str { todo!() } -/// # fn get_type_path(&self) -> &dyn DynamicTypePath { todo!() } /// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_any(self: Box) -> Box { todo!() } /// # fn as_any(&self) -> &dyn Any { todo!() } @@ -157,6 +160,10 @@ impl NonGenericTypeCell { /// # fn reflect_owned(self: Box) -> ReflectOwned { todo!() } /// # fn clone_value(&self) -> Box { todo!() } /// # } +/// # impl TypePath for Foo { +/// # fn type_path() -> &'static str { todo!() } +/// # fn short_type_path() -> &'static str { todo!() } +/// # } /// ``` /// /// Implementing [`TypePath`] with generics. @@ -180,9 +187,8 @@ impl NonGenericTypeCell { /// } /// } /// # -/// # impl Reflect for Foo { +/// # impl Reflect for Foo { /// # fn type_name(&self) -> &str { todo!() } -/// # fn get_type_path(&self) -> &dyn DynamicTypePath { todo!() } /// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_any(self: Box) -> Box { todo!() } /// # fn as_any(&self) -> &dyn Any { todo!() }