mirror of
https://github.com/bevyengine/bevy.git
synced 2026-06-30 07:45:50 -04:00
468acae854
# Objective Currently, type data needs to be in-scope when using `#[reflect(SomeType)]`. This can be mildly annoying when reflection is behind a feature since you need to duplicate the check at both the import and the usage: ```rust #[cfg(feature = "reflection")] use bevy_reflect::Reflect; #[cfg(feature = "reflection")] use crate::reflect::ReflectSomeType; #[cfg_attr(feature = "reflection", derive(Reflect), reflect(SomeType))] struct MyType; ``` You may also notice that it can be somewhat confusing that we import `ReflectSomeType` but write `reflect(SomeType)`. This is unfortunately due to a limitation in Rust, but it would be nice if users had the option at least of writing `reflect(ReflectSomeType)`—if only for consistency. ## Solution This PR enables registering type data via path rather than just identifier. ```rust #[cfg(feature = "reflection")] use bevy_reflect::Reflect; #[cfg_attr(feature = "reflection", derive(Reflect), reflect(crate::reflect::SomeType))] struct MyType; ``` Additionally, type data can now also be given by their `Reflect`-prefixed name. ```rust // Will register `super::reflect::ReflectSomeData` for `MyType` #[derive(Reflect)] #[reflect(super::reflect::SomeData)] struct MyType; // Will register `super::reflect::ReflectSomeData` for `MyOtherType` #[derive(Reflect)] #[reflect(super::reflect::ReflectSomeData)] struct MyOtherType; ``` ## Testing I added a unit test to ensure things compile correctly and both register the same data. --- ## Showcase Registering type data via the reflection derive macro can now be done by path: ```rust #[derive(Reflect)] // Will register `crate::equipment::ReflectEquippable` for `Sword` #[reflect(crate::equipment::Equippable)] struct Sword(i32); ``` Additionally, you may optionally used the `Reflect`-prefixed type name that the macro normally generates automatically: ```rust #[derive(Reflect)] // Will still register `crate::equipment::ReflectEquippable` for `Sword` #[reflect(crate::equipment::ReflectEquippable)] struct Sword(i32); ```