mirror of
https://github.com/bevyengine/bevy
synced 2024-12-21 10:33:08 +00:00
262846e702
# Objective
- Followup to #7184.
- ~Deprecate `TypeUuid` and remove its internal references.~ No longer
part of this PR.
- Use `TypePath` for the type registry, and (de)serialisation instead of
`std::any::type_name`.
- Allow accessing type path information behind proxies.
## Solution
- Introduce methods on `TypeInfo` and friends for dynamically querying
type path. These methods supersede the old `type_name` methods.
- Remove `Reflect::type_name` in favor of `DynamicTypePath::type_path`
and `TypeInfo::type_path_table`.
- Switch all uses of `std::any::type_name` in reflection, non-debugging
contexts to use `TypePath`.
---
## Changelog
- Added `TypePathTable` for dynamically accessing methods on `TypePath`
through `TypeInfo` and the type registry.
- Removed `type_name` from all `TypeInfo`-like structs.
- Added `type_path` and `type_path_table` methods to all `TypeInfo`-like
structs.
- Removed `Reflect::type_name` in favor of
`DynamicTypePath::reflect_type_path` and `TypeInfo::type_path`.
- Changed the signature of all `DynamicTypePath` methods to return
strings with a static lifetime.
## Migration Guide
- Rely on `TypePath` instead of `std::any::type_name` for all stability
guarantees and for use in all reflection contexts, this is used through
with one of the following APIs:
- `TypePath::type_path` if you have a concrete type and not a value.
- `DynamicTypePath::reflect_type_path` if you have an `dyn Reflect`
value without a concrete type.
- `TypeInfo::type_path` for use through the registry or if you want to
work with the represented type of a `DynamicFoo`.
- Remove `type_name` from manual `Reflect` implementations.
- Use `type_path` and `type_path_table` in place of `type_name` on
`TypeInfo`-like structs.
- Use `get_with_type_path(_mut)` over `get_with_type_name(_mut)`.
## Note to reviewers
I think if anything we were a little overzealous in merging #7184 and we
should take that extra care here.
In my mind, this is the "point of no return" for `TypePath` and while I
think we all agree on the design, we should carefully consider if the
finer details and current implementations are actually how we want them
moving forward.
For example [this incorrect `TypePath` implementation for
`String`](3fea3c6c0b/crates/bevy_reflect/src/impls/std.rs (L90)
)
(note that `String` is in the default Rust prelude) snuck in completely
under the radar.
135 lines
3.6 KiB
Rust
135 lines
3.6 KiB
Rust
use crate::{Reflect, TypePath, TypePathTable};
|
|
use std::any::{Any, TypeId};
|
|
|
|
/// The named field of a reflected struct.
|
|
#[derive(Clone, Debug)]
|
|
pub struct NamedField {
|
|
name: &'static str,
|
|
type_path: TypePathTable,
|
|
type_id: TypeId,
|
|
#[cfg(feature = "documentation")]
|
|
docs: Option<&'static str>,
|
|
}
|
|
|
|
impl NamedField {
|
|
/// Create a new [`NamedField`].
|
|
pub fn new<T: Reflect + TypePath>(name: &'static str) -> Self {
|
|
Self {
|
|
name,
|
|
type_path: TypePathTable::of::<T>(),
|
|
type_id: TypeId::of::<T>(),
|
|
#[cfg(feature = "documentation")]
|
|
docs: None,
|
|
}
|
|
}
|
|
|
|
/// Sets the docstring for this field.
|
|
#[cfg(feature = "documentation")]
|
|
pub fn with_docs(self, docs: Option<&'static str>) -> Self {
|
|
Self { docs, ..self }
|
|
}
|
|
|
|
/// The name of the field.
|
|
pub fn name(&self) -> &'static str {
|
|
self.name
|
|
}
|
|
|
|
/// A representation of the type path of the field.
|
|
///
|
|
/// 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 field.
|
|
///
|
|
/// 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 field.
|
|
pub fn type_id(&self) -> TypeId {
|
|
self.type_id
|
|
}
|
|
|
|
/// Check if the given type matches the field type.
|
|
pub fn is<T: Any>(&self) -> bool {
|
|
TypeId::of::<T>() == self.type_id
|
|
}
|
|
|
|
/// The docstring of this field, if any.
|
|
#[cfg(feature = "documentation")]
|
|
pub fn docs(&self) -> Option<&'static str> {
|
|
self.docs
|
|
}
|
|
}
|
|
|
|
/// The unnamed field of a reflected tuple or tuple struct.
|
|
#[derive(Clone, Debug)]
|
|
pub struct UnnamedField {
|
|
index: usize,
|
|
type_path: TypePathTable,
|
|
type_id: TypeId,
|
|
#[cfg(feature = "documentation")]
|
|
docs: Option<&'static str>,
|
|
}
|
|
|
|
impl UnnamedField {
|
|
pub fn new<T: Reflect + TypePath>(index: usize) -> Self {
|
|
Self {
|
|
index,
|
|
type_path: TypePathTable::of::<T>(),
|
|
type_id: TypeId::of::<T>(),
|
|
#[cfg(feature = "documentation")]
|
|
docs: None,
|
|
}
|
|
}
|
|
|
|
/// Sets the docstring for this field.
|
|
#[cfg(feature = "documentation")]
|
|
pub fn with_docs(self, docs: Option<&'static str>) -> Self {
|
|
Self { docs, ..self }
|
|
}
|
|
|
|
/// Returns the index of the field.
|
|
pub fn index(&self) -> usize {
|
|
self.index
|
|
}
|
|
|
|
/// A representation of the type path of the field.
|
|
///
|
|
/// 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 field.
|
|
///
|
|
/// 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 field.
|
|
pub fn type_id(&self) -> TypeId {
|
|
self.type_id
|
|
}
|
|
|
|
/// Check if the given type matches the field type.
|
|
pub fn is<T: Any>(&self) -> bool {
|
|
TypeId::of::<T>() == self.type_id
|
|
}
|
|
|
|
/// The docstring of this field, if any.
|
|
#[cfg(feature = "documentation")]
|
|
pub fn docs(&self) -> Option<&'static str> {
|
|
self.docs
|
|
}
|
|
}
|