@@ -59,6 +59,9 @@ pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value";
59
59
/// one for `ReflectFoo` and another for `ReflectBar`.
60
60
/// This assumes these types are indeed in-scope wherever this macro is called.
61
61
///
62
+ /// This is often used with traits that have been marked by the [`reflect_trait`] macro
63
+ /// in order to register the type's implementation of that trait.
64
+ ///
62
65
/// ### Special Identifiers
63
66
///
64
67
/// There are a few "special" identifiers that work a bit differently:
@@ -167,6 +170,55 @@ pub fn derive_type_uuid(input: TokenStream) -> TokenStream {
167
170
type_uuid:: type_uuid_derive ( input)
168
171
}
169
172
173
+ /// A macro that automatically generates type data for traits, which their implementors can then register.
174
+ ///
175
+ /// The output of this macro is a struct that takes reflected instances of the implementor's type
176
+ /// and returns the value as a trait object.
177
+ /// Because of this, **it can only be used on [object-safe] traits.**
178
+ ///
179
+ /// For a trait named `MyTrait`, this will generate the struct `ReflectMyTrait`.
180
+ /// The generated struct can be created using `FromType` with any type that implements the trait.
181
+ /// The creation and registration of this generated struct as type data can be automatically handled
182
+ /// by the [`Reflect` derive macro].
183
+ ///
184
+ /// # Example
185
+ ///
186
+ /// ```ignore
187
+ /// # use std::any::TypeId;
188
+ /// # use bevy_reflect_derive::{Reflect, reflect_trait};
189
+ /// #[reflect_trait] // Generates `ReflectMyTrait`
190
+ /// trait MyTrait {
191
+ /// fn print(&self) -> &str;
192
+ /// }
193
+ ///
194
+ /// #[derive(Reflect)]
195
+ /// #[reflect(MyTrait)] // Automatically registers `ReflectMyTrait`
196
+ /// struct SomeStruct;
197
+ ///
198
+ /// impl MyTrait for SomeStruct {
199
+ /// fn print(&self) -> &str {
200
+ /// "Hello, World!"
201
+ /// }
202
+ /// }
203
+ ///
204
+ /// // We can create the type data manually if we wanted:
205
+ /// let my_trait: ReflectMyTrait = FromType::<SomeStruct>::from_type();
206
+ ///
207
+ /// // Or we can simply get it from the registry:
208
+ /// let mut registry = TypeRegistry::default();
209
+ /// registry.register::<SomeStruct>();
210
+ /// let my_trait = registry
211
+ /// .get_type_data::<ReflectMyTrait>(TypeId::of::<SomeStruct>())
212
+ /// .unwrap();
213
+ ///
214
+ /// // Then use it on reflected data
215
+ /// let reflected: Box<dyn Reflect> = Box::new(SomeStruct);
216
+ /// let reflected_my_trait: &dyn MyTrait = my_trait.get(&*reflected).unwrap();
217
+ /// assert_eq!("Hello, World!", reflected_my_trait.print());
218
+ /// ```
219
+ ///
220
+ /// [object-safe]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
221
+ /// [`Reflect` derive macro]: Reflect
170
222
#[ proc_macro_attribute]
171
223
pub fn reflect_trait ( args : TokenStream , input : TokenStream ) -> TokenStream {
172
224
trait_reflection:: reflect_trait ( & args, input)
0 commit comments