@@ -40,9 +40,16 @@ impl Debug for TypeRegistryArc {
40
40
}
41
41
}
42
42
43
- /// A trait which allows a type to generate its [`TypeRegistration`].
43
+ /// A trait which allows a type to generate its [`TypeRegistration`]
44
+ /// for registration into the [`TypeRegistry`].
44
45
///
45
- /// This trait is automatically implemented for types which derive [`Reflect`].
46
+ /// This trait is automatically implemented for items using the [`Reflect` derive macro].
47
+ /// The macro also allows [`TypeData`] to be more easily registered.
48
+ ///
49
+ /// See the [crate-level documentation] for more information on type registration.
50
+ ///
51
+ /// [`Reflect` derive macro]: bevy_reflect_derive::Reflect
52
+ /// [crate-level documentation]: crate
46
53
pub trait GetTypeRegistration {
47
54
fn get_type_registration ( ) -> TypeRegistration ;
48
55
}
@@ -269,19 +276,33 @@ impl TypeRegistryArc {
269
276
}
270
277
}
271
278
272
- /// A record of data about a type.
279
+ /// Runtime storage for type metadata, registered into the [`TypeRegistry`].
280
+ ///
281
+ /// An instance of `TypeRegistration` can be created using the [`TypeRegistration::of`] method,
282
+ /// but is more often automatically generated using the [`Reflect` derive macro] which itself generates
283
+ /// an implementation of the [`GetTypeRegistration`] trait.
273
284
///
274
- /// This contains the [`TypeInfo`] of the type, as well as its [short name].
285
+ /// Along with the type's [`TypeInfo`] and [short name],
286
+ /// this struct also contains a type's registered [`TypeData`].
287
+ ///
288
+ /// See the [crate-level documentation] for more information on type registration.
289
+ ///
290
+ /// # Example
291
+ ///
292
+ /// ```
293
+ /// # use bevy_reflect::{TypeRegistration, std_traits::ReflectDefault, FromType};
294
+ /// let mut registration = TypeRegistration::of::<Option<String>>();
275
295
///
276
- /// For each trait specified by the [`#[reflect(_)]`][0] attribute of
277
- /// [`#[derive(Reflect)]`][1] on the registered type, this record also contains
278
- /// a [`TypeData`] which can be used to downcast [`Reflect`] trait objects of
279
- /// this type to trait objects of the relevant trait.
296
+ /// assert_eq!("core::option::Option<alloc::string::String>", registration.type_name());
297
+ /// assert_eq!("Option<String>", registration.short_name());
280
298
///
299
+ /// registration.insert::<ReflectDefault>(FromType::<Option<String>>::from_type());
300
+ /// assert!(registration.data::<ReflectDefault>().is_some())
301
+ /// ```
302
+ ///
303
+ /// [`Reflect` derive macro]: bevy_reflect_derive::Reflect
281
304
/// [short name]: bevy_utils::get_short_name
282
- /// [`TypeInfo`]: crate::TypeInfo
283
- /// [0]: crate::Reflect
284
- /// [1]: crate::Reflect
305
+ /// [crate-level documentation]: crate
285
306
pub struct TypeRegistration {
286
307
short_name : String ,
287
308
data : HashMap < TypeId , Box < dyn TypeData > > ,
@@ -377,9 +398,19 @@ impl Clone for TypeRegistration {
377
398
}
378
399
}
379
400
}
380
- /// A trait for types generated by the [`#[reflect_trait]`][0] attribute macro.
401
+
402
+ /// A trait used to type-erase type metadata.
403
+ ///
404
+ /// Type data can be registered to the [`TypeRegistry`] and stored on a type's [`TypeRegistration`].
405
+ ///
406
+ /// While type data is often generated using the [`#[reflect_trait]`][0] macro,
407
+ /// almost any type that implements [`Clone`] can be considered "type data".
408
+ /// This is because it has a blanket implementation over all `T` where `T: Clone + Send + Sync + 'static`.
409
+ ///
410
+ /// See the [crate-level documentation] for more information on type data and type registration.
381
411
///
382
412
/// [0]: crate::reflect_trait
413
+ /// [crate-level documentation]: crate
383
414
pub trait TypeData : Downcast + Send + Sync {
384
415
fn clone_type_data ( & self ) -> Box < dyn TypeData > ;
385
416
}
0 commit comments