Skip to content

Commit 1efc762

Browse files
authored
reflect: stable type path v2 (#7184)
# Objective - Introduce a stable alternative to [`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html). - Rewrite of #5805 with heavy inspiration in design. - On the path to #5830. - Part of solving #3327. ## Solution - Add a `TypePath` trait for static stable type path/name information. - Add a `TypePath` derive macro. - Add a `impl_type_path` macro for implementing internal and foreign types in `bevy_reflect`. --- ## Changelog - Added `TypePath` trait. - Added `DynamicTypePath` trait and `get_type_path` method to `Reflect`. - Added a `TypePath` derive macro. - Added a `bevy_reflect::impl_type_path` for implementing `TypePath` on internal and foreign types in `bevy_reflect`. - Changed `bevy_reflect::utility::(Non)GenericTypeInfoCell` to `(Non)GenericTypedCell<T>` which allows us to be generic over both `TypeInfo` and `TypePath`. - `TypePath` is now a supertrait of `Asset`, `Material` and `Material2d`. - `impl_reflect_struct` needs a `#[type_path = "..."]` attribute to be specified. - `impl_reflect_value` needs to either specify path starting with a double colon (`::core::option::Option`) or an `in my_crate::foo` declaration. - Added `bevy_reflect_derive::ReflectTypePath`. - Most uses of `Ident` in `bevy_reflect_derive` changed to use `ReflectTypePath`. ## Migration Guide - Implementors of `Asset`, `Material` and `Material2d` now also need to derive `TypePath`. - Manual implementors of `Reflect` will need to implement the new `get_type_path` method. ## Open Questions - [x] ~This PR currently does not migrate any usages of `std::any::type_name` to use `bevy_reflect::TypePath` to ease the review process. Should it?~ Migration will be left to a follow-up PR. - [ ] This PR adds a lot of `#[derive(TypePath)]` and `T: TypePath` to satisfy new bounds, mostly when deriving `TypeUuid`. Should we make `TypePath` a supertrait of `TypeUuid`? [Should we remove `TypeUuid` in favour of `TypePath`?](https://github.com/bevyengine/bevy/pull/5805/files/2afbd855327c4b68e0a6b6f03118f289988441a4#r961067892)
1 parent 94dce09 commit 1efc762

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2062
-411
lines changed

crates/bevy_asset/src/asset_server.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub struct AssetServerInternal {
9898
/// use bevy_asset::{AssetServer, Handle};
9999
/// use bevy_ecs::prelude::{Commands, Res};
100100
///
101-
/// # #[derive(Debug, bevy_reflect::TypeUuid)]
101+
/// # #[derive(Debug, bevy_reflect::TypeUuid, bevy_reflect::TypePath)]
102102
/// # #[uuid = "00000000-0000-0000-0000-000000000000"]
103103
/// # struct Image;
104104
///
@@ -647,10 +647,10 @@ mod test {
647647
use crate::{loader::LoadedAsset, update_asset_storage_system};
648648
use bevy_app::{App, Update};
649649
use bevy_ecs::prelude::*;
650-
use bevy_reflect::TypeUuid;
650+
use bevy_reflect::{TypePath, TypeUuid};
651651
use bevy_utils::BoxedFuture;
652652

653-
#[derive(Debug, TypeUuid)]
653+
#[derive(Debug, TypeUuid, TypePath)]
654654
#[uuid = "a5189b72-0572-4290-a2e0-96f73a491c44"]
655655
struct PngAsset;
656656

crates/bevy_asset/src/assets.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ mod tests {
488488

489489
#[test]
490490
fn asset_overwriting() {
491-
#[derive(bevy_reflect::TypeUuid)]
491+
#[derive(bevy_reflect::TypeUuid, bevy_reflect::TypePath)]
492492
#[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"]
493493
struct MyAsset;
494494
let mut app = App::new();

crates/bevy_asset/src/loader.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
use anyhow::Error;
66
use anyhow::Result;
77
use bevy_ecs::system::{Res, ResMut};
8+
use bevy_reflect::TypePath;
89
use bevy_reflect::{TypeUuid, TypeUuidDynamic};
910
use bevy_utils::{BoxedFuture, HashMap};
1011
use crossbeam_channel::{Receiver, Sender};
@@ -47,13 +48,13 @@ pub trait AssetLoader: Send + Sync + 'static {
4748
///
4849
/// In order to load assets into your game you must either add them manually to an asset storage
4950
/// with [`Assets::add`] or load them from the filesystem with [`AssetServer::load`].
50-
pub trait Asset: TypeUuid + AssetDynamic {}
51+
pub trait Asset: TypeUuid + TypePath + AssetDynamic {}
5152

5253
/// An untyped version of the [`Asset`] trait.
5354
pub trait AssetDynamic: Downcast + TypeUuidDynamic + Send + Sync + 'static {}
5455
impl_downcast!(AssetDynamic);
5556

56-
impl<T> Asset for T where T: TypeUuid + AssetDynamic + TypeUuidDynamic {}
57+
impl<T> Asset for T where T: TypeUuid + TypePath + AssetDynamic + TypeUuidDynamic {}
5758

5859
impl<T> AssetDynamic for T where T: Send + Sync + 'static + TypeUuidDynamic {}
5960

crates/bevy_audio/src/audio_source.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use anyhow::Result;
22
use bevy_asset::{Asset, AssetLoader, LoadContext, LoadedAsset};
3-
use bevy_reflect::TypeUuid;
3+
use bevy_reflect::{TypePath, TypeUuid};
44
use bevy_utils::BoxedFuture;
55
use std::{io::Cursor, sync::Arc};
66

77
/// A source of audio data
8-
#[derive(Debug, Clone, TypeUuid)]
8+
#[derive(Debug, Clone, TypeUuid, TypePath)]
99
#[uuid = "7a14806a-672b-443b-8d16-4f18afefa463"]
1010
pub struct AudioSource {
1111
/// Raw data of the audio source.

crates/bevy_audio/src/sinks.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bevy_math::Vec3;
2-
use bevy_reflect::TypeUuid;
2+
use bevy_reflect::{TypePath, TypeUuid};
33
use bevy_transform::prelude::Transform;
44
use rodio::{Sink, SpatialSink};
55

@@ -87,7 +87,7 @@ pub trait AudioSinkPlayback {
8787
/// }
8888
/// ```
8989
///
90-
#[derive(TypeUuid)]
90+
#[derive(TypePath, TypeUuid)]
9191
#[uuid = "8BEE570C-57C2-4FC0-8CFB-983A22F7D981"]
9292
pub struct AudioSink {
9393
// This field is an Option in order to allow us to have a safe drop that will detach the sink.
@@ -158,7 +158,7 @@ impl AudioSinkPlayback for AudioSink {
158158
/// }
159159
/// ```
160160
///
161-
#[derive(TypeUuid)]
161+
#[derive(TypePath, TypeUuid)]
162162
#[uuid = "F3CA4C47-595E-453B-96A7-31C3DDF2A177"]
163163
pub struct SpatialAudioSink {
164164
// This field is an Option in order to allow us to have a safe drop that will detach the sink.

crates/bevy_ecs/src/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
407407
}
408408
}
409409

410-
impl_reflect_value!(Entity(Hash, PartialEq, Serialize, Deserialize));
410+
impl_reflect_value!((in bevy_ecs) Entity(Hash, PartialEq, Serialize, Deserialize));
411411
impl_from_reflect_value!(Entity);
412412

413413
#[derive(Clone)]

crates/bevy_gltf/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy_app::prelude::*;
1212
use bevy_asset::{AddAsset, Handle};
1313
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
1414
use bevy_pbr::StandardMaterial;
15-
use bevy_reflect::{Reflect, TypeUuid};
15+
use bevy_reflect::{Reflect, TypePath, TypeUuid};
1616
use bevy_render::{
1717
mesh::{Mesh, MeshVertexAttribute},
1818
renderer::RenderDevice,
@@ -58,7 +58,7 @@ impl Plugin for GltfPlugin {
5858
}
5959

6060
/// Representation of a loaded glTF file.
61-
#[derive(Debug, TypeUuid)]
61+
#[derive(Debug, TypeUuid, TypePath)]
6262
#[uuid = "5c7d5f8a-f7b0-4e45-a09e-406c0372fea2"]
6363
pub struct Gltf {
6464
pub scenes: Vec<Handle<Scene>>,
@@ -78,7 +78,7 @@ pub struct Gltf {
7878

7979
/// A glTF node with all of its child nodes, its [`GltfMesh`],
8080
/// [`Transform`](bevy_transform::prelude::Transform) and an optional [`GltfExtras`].
81-
#[derive(Debug, Clone, TypeUuid)]
81+
#[derive(Debug, Clone, TypeUuid, TypePath)]
8282
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
8383
pub struct GltfNode {
8484
pub children: Vec<GltfNode>,
@@ -89,15 +89,15 @@ pub struct GltfNode {
8989

9090
/// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive)
9191
/// and an optional [`GltfExtras`].
92-
#[derive(Debug, Clone, TypeUuid)]
92+
#[derive(Debug, Clone, TypeUuid, TypePath)]
9393
#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"]
9494
pub struct GltfMesh {
9595
pub primitives: Vec<GltfPrimitive>,
9696
pub extras: Option<GltfExtras>,
9797
}
9898

9999
/// Part of a [`GltfMesh`] that consists of a [`Mesh`], an optional [`StandardMaterial`] and [`GltfExtras`].
100-
#[derive(Debug, Clone, TypeUuid)]
100+
#[derive(Debug, Clone, TypeUuid, TypePath)]
101101
#[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"]
102102
pub struct GltfPrimitive {
103103
pub mesh: Handle<Mesh>,

crates/bevy_input/src/input.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,12 @@ where
174174

175175
#[cfg(test)]
176176
mod test {
177+
use bevy_reflect::TypePath;
178+
177179
use crate::Input;
178180

179181
/// Used for testing the functionality of [`Input`].
180-
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
182+
#[derive(TypePath, Copy, Clone, Eq, PartialEq, Hash)]
181183
enum DummyInput {
182184
Input1,
183185
Input2,

crates/bevy_pbr/src/material.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bevy_ecs::{
1919
SystemParamItem,
2020
},
2121
};
22-
use bevy_reflect::TypeUuid;
22+
use bevy_reflect::{TypePath, TypeUuid};
2323
use bevy_render::{
2424
extract_component::ExtractComponentPlugin,
2525
mesh::{Mesh, MeshVertexBufferLayout},
@@ -59,11 +59,11 @@ use std::marker::PhantomData;
5959
/// ```
6060
/// # use bevy_pbr::{Material, MaterialMeshBundle};
6161
/// # use bevy_ecs::prelude::*;
62-
/// # use bevy_reflect::TypeUuid;
62+
/// # use bevy_reflect::{TypeUuid, TypePath};
6363
/// # use bevy_render::{render_resource::{AsBindGroup, ShaderRef}, texture::Image, color::Color};
6464
/// # use bevy_asset::{Handle, AssetServer, Assets};
6565
///
66-
/// #[derive(AsBindGroup, TypeUuid, Debug, Clone)]
66+
/// #[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone)]
6767
/// #[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
6868
/// pub struct CustomMaterial {
6969
/// // Uniform bindings must implement `ShaderType`, which will be used to convert the value to
@@ -106,7 +106,7 @@ use std::marker::PhantomData;
106106
/// @group(1) @binding(2)
107107
/// var color_sampler: sampler;
108108
/// ```
109-
pub trait Material: AsBindGroup + Send + Sync + Clone + TypeUuid + Sized + 'static {
109+
pub trait Material: AsBindGroup + Send + Sync + Clone + TypeUuid + TypePath + Sized {
110110
/// Returns this material's vertex shader. If [`ShaderRef::Default`] is returned, the default mesh vertex shader
111111
/// will be used.
112112
fn vertex_shader() -> ShaderRef {

0 commit comments

Comments
 (0)