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.
This commit is contained in:
Carter Anderson 2023-06-06 12:23:58 -05:00 committed by GitHub
parent 89cbc78d3d
commit 8b9d88f4d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 36 additions and 143 deletions

View file

@ -199,11 +199,6 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath {
self
}
#[inline]
fn into_any(self: #FQBox<Self>) -> #FQBox<dyn #FQAny> {
self

View file

@ -174,11 +174,6 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath {
self
}
#[inline]
fn into_any(self: #FQBox<Self>) -> #FQBox<dyn #FQAny> {
self

View file

@ -144,11 +144,6 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath {
self
}
#[inline]
fn into_any(self: #FQBox<Self>) -> #FQBox<dyn #FQAny> {
self

View file

@ -56,11 +56,6 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn #bevy_reflect_path::DynamicTypePath {
self
}
#[inline]
fn into_any(self: #FQBox<Self>) -> #FQBox<dyn #FQAny> {
self

View file

@ -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<Self>) -> Box<dyn Any> {
self

View file

@ -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<Self>) -> Box<dyn Any> {
self

View file

@ -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<T: smallvec::Array + TypePath + Send + Sync> List for SmallVec<T>
@ -88,11 +88,6 @@ where
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}

View file

@ -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(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
@ -555,10 +550,6 @@ macro_rules! impl_reflect_for_hashmap {
Some(<Self as Typed>::type_info())
}
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
@ -721,11 +712,6 @@ impl<T: Reflect + TypePath, const N: usize> Reflect for [T; N] {
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
#[inline]
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
@ -943,11 +929,6 @@ impl<T: FromReflect + TypePath> Reflect for Option<T> {
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
#[inline]
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
@ -1115,11 +1096,6 @@ impl Reflect for Cow<'static, str> {
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
@ -1252,11 +1228,6 @@ impl Reflect for &'static Path {
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
@ -1366,11 +1337,6 @@ impl Reflect for Cow<'static, Path> {
std::any::type_name::<Self>()
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Some(<Self as Typed>::type_info())
}

View file

@ -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<Self>) -> Box<dyn Any> {
self

View file

@ -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<Self>) -> Box<dyn Any> {
self
}

View file

@ -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<dyn Any>`][std::any::Any].
fn into_any(self: Box<Self>) -> Box<dyn Any>;

View file

@ -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<Self>) -> Box<dyn Any> {
self

View file

@ -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<Self>) -> Box<dyn Any> {
self
@ -538,11 +533,6 @@ macro_rules! impl_reflect_tuple {
Some(<Self as Typed>::type_info())
}
#[inline]
fn get_type_path(&self) -> &dyn DynamicTypePath {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}

View file

@ -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<Self>) -> Box<dyn Any> {
self

View file

@ -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<Self>) -> Box<dyn Any> { 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<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
/// # }
/// #
/// # impl TypePath for MyStruct {
/// # fn type_path() -> &'static str { todo!() }
/// # fn short_type_path() -> &'static str { todo!() }
/// # }
/// ```
///
/// [utility]: crate::utility

View file

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

View file

@ -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<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }
@ -85,6 +84,11 @@ mod sealed {
/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { 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<T: TypedProperty> NonGenericTypeCell<T> {
///
/// ```
/// # 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>(T);
@ -142,7 +146,6 @@ impl<T: TypedProperty> NonGenericTypeCell<T> {
/// #
/// # impl<T: Reflect> Reflect for Foo<T> {
/// # 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<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }
@ -157,6 +160,10 @@ impl<T: TypedProperty> NonGenericTypeCell<T> {
/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
/// # }
/// # impl<T: Reflect> TypePath for Foo<T> {
/// # fn type_path() -> &'static str { todo!() }
/// # fn short_type_path() -> &'static str { todo!() }
/// # }
/// ```
///
/// Implementing [`TypePath`] with generics.
@ -180,9 +187,8 @@ impl<T: TypedProperty> NonGenericTypeCell<T> {
/// }
/// }
/// #
/// # impl<T: Reflect> Reflect for Foo<T> {
/// # impl<T: Reflect + TypePath> Reflect for Foo<T> {
/// # 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<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }