Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect: TypePath part 2 #8768

Merged
merged 32 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
10bd53e
removed all reflect-scoped std::any::type_name usages
soqb Jun 6, 2023
5a9a5ff
regular tests
soqb Jun 6, 2023
0fd5e36
doctests!
soqb Jun 6, 2023
6792bb1
docs and collision resistance
soqb Jun 6, 2023
e6c6e3f
Merge remote-tracking branch 'bevy/main' into HEAD
soqb Jun 23, 2023
fa544af
merge fixes
soqb Jun 6, 2023
0e94e7e
remove extra println
soqb Jun 6, 2023
8cb80ea
docs and tests
soqb Jun 6, 2023
6768e05
maximally relax bounds for all TypePath impls
soqb Jun 6, 2023
4a9e902
`Typed: TypePath` and where clause lifetime bounds
soqb Jun 6, 2023
4e8dfa7
various correctness fixes
soqb Jun 23, 2023
469c927
Merge branch 'main' of https://github.com/bevyengine/bevy into HEAD
soqb Jun 23, 2023
bd0b407
merge fixes
soqb Jun 23, 2023
edc0d43
documentation fixes
soqb Jun 23, 2023
a6b3b1c
`cargo fmt --all`
soqb Jun 23, 2023
6a5ed35
fix doc test
soqb Jun 23, 2023
5e2ac16
fix doc slips
soqb Jun 26, 2023
074d769
remove `DynamicTuple::name` that was never used
soqb Jun 26, 2023
4c68253
change more docs
soqb Jun 26, 2023
71ad523
fix tuple `TypePath` impl
soqb Jun 26, 2023
e42152f
add slices to new test
soqb Jun 26, 2023
c2689a3
merge with main
soqb Aug 15, 2023
6057a4f
vtable -> table
soqb Aug 15, 2023
bce5c9d
address feedback
soqb Aug 19, 2023
2a4442f
remove unused import
soqb Aug 19, 2023
c1bc5cc
appease clippy
soqb Aug 19, 2023
a3dbbd5
readded via deprecation `Reflect::type_name`
soqb Sep 22, 2023
b6b9af8
aaagh!
soqb Sep 22, 2023
9d49b3a
`TypePath` for &mut
soqb Sep 22, 2023
eca4e38
Merge branch 'main' of https://github.com/bevyengine/bevy into reflec…
soqb Sep 22, 2023
aae3ca3
add missing doc link
soqb Sep 22, 2023
0b533c0
Merge branch 'main' of https://github.com/bevyengine/bevy into reflec…
soqb Oct 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ impl App {
/// See [`bevy_reflect::TypeRegistry::register_type_data`].
#[cfg(feature = "bevy_reflect")]
pub fn register_type_data<
T: bevy_reflect::Reflect + 'static,
T: bevy_reflect::Reflect + bevy_reflect::TypePath,
D: bevy_reflect::TypeData + bevy_reflect::FromType<T>,
>(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl AssetServer {
.is_some()
{
panic!("Error while registering new asset type: {:?} with UUID: {:?}. Another type with the same UUID is already registered. Can not register new asset type with the same UUID",
std::any::type_name::<T>(), T::TYPE_UUID);
T::type_path(), T::TYPE_UUID);
}
Assets::new(self.server.asset_ref_counter.channel.sender.clone())
}
Expand Down
15 changes: 3 additions & 12 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,15 @@ impl<T: Asset> Debug for AssetEvent<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetEvent::Created { handle } => f
.debug_struct(&format!(
"AssetEvent<{}>::Created",
std::any::type_name::<T>()
))
.debug_struct(&format!("AssetEvent<{}>::Created", T::type_path()))
.field("handle", &handle.id())
.finish(),
AssetEvent::Modified { handle } => f
.debug_struct(&format!(
"AssetEvent<{}>::Modified",
std::any::type_name::<T>()
))
.debug_struct(&format!("AssetEvent<{}>::Modified", T::type_path()))
.field("handle", &handle.id())
.finish(),
AssetEvent::Removed { handle } => f
.debug_struct(&format!(
"AssetEvent<{}>::Removed",
std::any::type_name::<T>()
))
.debug_struct(&format!("AssetEvent<{}>::Removed", T::type_path()))
.field("handle", &handle.id())
.finish(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<T: Asset> AssetCountDiagnosticsPlugin<T> {

/// Registers the asset count diagnostic for the current application.
pub fn setup_system(mut diagnostics: ResMut<DiagnosticsStore>) {
let asset_type_name = std::any::type_name::<T>();
let asset_type_name = T::type_path();
let max_length = MAX_DIAGNOSTIC_NAME_WIDTH - "asset_count ".len();
diagnostics.add(Diagnostic::new(
Self::diagnostic_id(),
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,13 @@ impl<T: Asset> Default for Handle<T> {

impl<T: Asset> Debug for Handle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let name = std::any::type_name::<T>().split("::").last().unwrap();
write!(f, "{:?}Handle<{name}>({:?})", self.handle_type, self.id)
write!(
f,
"{:?}Handle<{}>({:?})",
self.handle_type,
T::short_type_path(),
self.id
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ pub(crate) enum ReflectDerive<'a> {
/// // traits
/// // |----------------------------------------|
/// #[reflect(PartialEq, Serialize, Deserialize, Default)]
/// // type_name generics
/// // type_path generics
soqb marked this conversation as resolved.
Show resolved Hide resolved
/// // |-------------------||----------|
/// struct ThingThatImReflecting<T1, T2, T3> {/* ... */}
/// ```
pub(crate) struct ReflectMeta<'a> {
/// The registered traits for this type.
traits: ReflectTraits,
/// The name of this type.
/// The path to this type.
type_path: ReflectTypePath<'a>,
/// A cached instance of the path to the `bevy_reflect` crate.
bevy_reflect_path: Path,
Expand Down Expand Up @@ -387,7 +387,7 @@ impl<'a> ReflectMeta<'a> {
self.traits.from_reflect_attrs()
}

/// The name of this struct.
/// The path to this type.
pub fn type_path(&self) -> &ReflectTypePath<'a> {
&self.type_path
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
if let #bevy_reflect_path::ReflectRef::Enum(#ref_value) = #bevy_reflect_path::Reflect::reflect_ref(#ref_value) {
match #bevy_reflect_path::Enum::variant_name(#ref_value) {
#(#variant_names => #fqoption::Some(#variant_constructors),)*
name => panic!("variant with name `{}` does not exist on enum `{}`", name, ::core::any::type_name::<Self>()),
name => panic!("variant with name `{}` does not exist on enum `{}`", name, <Self as #bevy_reflect_path::TypePath>::type_path()),
}
} else {
#FQOption::None
Expand Down
15 changes: 4 additions & 11 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,18 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
}
});

let string_name = enum_path.get_ident().unwrap().to_string();

#[cfg(feature = "documentation")]
let info_generator = {
let doc = reflect_enum.meta().doc();
quote! {
#bevy_reflect_path::EnumInfo::new::<Self>(#string_name, &variants).with_docs(#doc)
#bevy_reflect_path::EnumInfo::new::<Self>(&variants).with_docs(#doc)
}
};

#[cfg(not(feature = "documentation"))]
let info_generator = {
quote! {
#bevy_reflect_path::EnumInfo::new::<Self>(#string_name, &variants)
#bevy_reflect_path::EnumInfo::new::<Self>(&variants)
}
};

Expand Down Expand Up @@ -188,11 +186,6 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
}

impl #impl_generics #bevy_reflect_path::Reflect for #enum_path #ty_generics #where_reflect_clause {
#[inline]
fn type_name(&self) -> &str {
::core::any::type_name::<Self>()
}

#[inline]
fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
Expand Down Expand Up @@ -264,11 +257,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
#(#variant_names => {
*self = #variant_constructors
})*
name => panic!("variant with name `{}` does not exist on enum `{}`", name, ::core::any::type_name::<Self>()),
name => panic!("variant with name `{}` does not exist on enum `{}`", name, <Self as #bevy_reflect_path::TypePath>::type_path()),
}
}
} else {
panic!("`{}` is not an enum", #bevy_reflect_path::Reflect::type_name(#ref_value));
panic!("`{}` is not an enum", #bevy_reflect_path::DynamicTypePath::reflect_type_path(#ref_value));
}
}

Expand Down
11 changes: 2 additions & 9 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,18 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS
}
};

let string_name = struct_path.get_ident().unwrap().to_string();

#[cfg(feature = "documentation")]
let info_generator = {
let doc = reflect_struct.meta().doc();
quote! {
#bevy_reflect_path::StructInfo::new::<Self>(#string_name, &fields).with_docs(#doc)
#bevy_reflect_path::StructInfo::new::<Self>(&fields).with_docs(#doc)
}
};

#[cfg(not(feature = "documentation"))]
let info_generator = {
quote! {
#bevy_reflect_path::StructInfo::new::<Self>(#string_name, &fields)
#bevy_reflect_path::StructInfo::new::<Self>(&fields)
}
};

Expand Down Expand Up @@ -163,11 +161,6 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS
}

impl #impl_generics #bevy_reflect_path::Reflect for #struct_path #ty_generics #where_reflect_clause {
#[inline]
fn type_name(&self) -> &str {
::core::any::type_name::<Self>()
}

#[inline]
fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,18 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::
}
};

let string_name = struct_path.get_ident().unwrap().to_string();

#[cfg(feature = "documentation")]
let info_generator = {
let doc = reflect_struct.meta().doc();
quote! {
#bevy_reflect_path::TupleStructInfo::new::<Self>(#string_name, &fields).with_docs(#doc)
#bevy_reflect_path::TupleStructInfo::new::<Self>(&fields).with_docs(#doc)
}
};

#[cfg(not(feature = "documentation"))]
let info_generator = {
quote! {
#bevy_reflect_path::TupleStructInfo::new::<Self>(#string_name, &fields)
#bevy_reflect_path::TupleStructInfo::new::<Self>(&fields)
}
};

Expand Down Expand Up @@ -133,11 +131,6 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::
}

impl #impl_generics #bevy_reflect_path::Reflect for #struct_path #ty_generics #where_reflect_clause {
#[inline]
fn type_name(&self) -> &str {
::core::any::type_name::<Self>()
}

#[inline]
fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
Expand Down
7 changes: 1 addition & 6 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
#typed_impl

impl #impl_generics #bevy_reflect_path::Reflect for #type_path #ty_generics #where_reflect_clause {
#[inline]
fn type_name(&self) -> &str {
::core::any::type_name::<Self>()
}

#[inline]
fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> {
#FQOption::Some(<Self as #bevy_reflect_path::Typed>::type_info())
Expand Down Expand Up @@ -96,7 +91,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
if let #FQOption::Some(value) = <dyn #FQAny>::downcast_ref::<Self>(value) {
*self = #FQClone::clone(value);
} else {
panic!("Value is not {}.", ::core::any::type_name::<Self>());
panic!("Value is not {}.", <Self as #bevy_reflect_path::TypePath>::type_path());
}
}

Expand Down
45 changes: 24 additions & 21 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
self as bevy_reflect, utility::reflect_hasher, Reflect, ReflectMut, ReflectOwned, ReflectRef,
TypeInfo,
TypeInfo, TypePath, TypePathTable,
};
use bevy_reflect_derive::impl_type_path;
use std::{
Expand Down Expand Up @@ -77,9 +77,9 @@ pub trait Array: Reflect {
/// A container for compile-time array info.
#[derive(Clone, Debug)]
pub struct ArrayInfo {
type_name: &'static str,
type_path: TypePathTable,
type_id: TypeId,
item_type_name: &'static str,
item_type_path: TypePathTable,
item_type_id: TypeId,
capacity: usize,
#[cfg(feature = "documentation")]
Expand All @@ -93,11 +93,11 @@ impl ArrayInfo {
///
/// * `capacity`: The maximum capacity of the underlying array.
///
pub fn new<TArray: Array, TItem: Reflect>(capacity: usize) -> Self {
pub fn new<TArray: Array + TypePath, TItem: Reflect + TypePath>(capacity: usize) -> Self {
Self {
type_name: std::any::type_name::<TArray>(),
type_path: TypePathTable::of::<TArray>(),
type_id: TypeId::of::<TArray>(),
item_type_name: std::any::type_name::<TItem>(),
item_type_path: TypePathTable::of::<TItem>(),
item_type_id: TypeId::of::<TItem>(),
capacity,
#[cfg(feature = "documentation")]
Expand All @@ -116,11 +116,21 @@ impl ArrayInfo {
self.capacity
}

/// The [type name] of the array.
/// A representation of the type path of the array.
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
/// Provides dynamic access to all methods on [`TypePath`].
pub fn type_path_table(&self) -> &TypePathTable {
&self.type_path
}

/// The [stable, full type path] of the array.
///
/// Use [`type_path_table`] if you need access to the other methods on [`TypePath`].
///
/// [stable, full type path]: TypePath
/// [`type_path_table`]: Self::type_path_table
pub fn type_path(&self) -> &'static str {
self.type_path_table().path()
}

/// The [`TypeId`] of the array.
Expand All @@ -133,11 +143,11 @@ impl ArrayInfo {
TypeId::of::<T>() == self.type_id
}

/// The [type name] of the array item.
/// A representation of the type path of the array item.
///
/// [type name]: std::any::type_name
pub fn item_type_name(&self) -> &'static str {
self.item_type_name
/// Provides dynamic access to all methods on [`TypePath`].
pub fn item_type_path_table(&self) -> &TypePathTable {
&self.item_type_path
}

/// The [`TypeId`] of the array item.
Expand Down Expand Up @@ -213,13 +223,6 @@ impl DynamicArray {
}

impl Reflect for DynamicArray {
#[inline]
fn type_name(&self) -> &str {
self.represented_type
.map(|info| info.type_name())
.unwrap_or_else(|| std::any::type_name::<Self>())
}

#[inline]
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
self.represented_type
Expand Down
9 changes: 1 addition & 8 deletions crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,6 @@ impl Enum for DynamicEnum {
}

impl Reflect for DynamicEnum {
#[inline]
fn type_name(&self) -> &str {
self.represented_type
.map(|info| info.type_name())
.unwrap_or_default()
}

#[inline]
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
self.represented_type
Expand Down Expand Up @@ -376,7 +369,7 @@ impl Reflect for DynamicEnum {
self.set_variant(value.variant_name(), dyn_variant);
}
} else {
panic!("`{}` is not an enum", value.type_name());
panic!("`{}` is not an enum", value.reflect_type_path());
}
}

Expand Down
Loading