Skip to content

Commit a228a84

Browse files
committed
Document type registration items
1 parent 71de41c commit a228a84

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

crates/bevy_reflect/src/type_registry.rs

+43-12
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ impl Debug for TypeRegistryArc {
4040
}
4141
}
4242

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`].
4445
///
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
4653
pub trait GetTypeRegistration {
4754
fn get_type_registration() -> TypeRegistration;
4855
}
@@ -269,19 +276,33 @@ impl TypeRegistryArc {
269276
}
270277
}
271278

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.
273284
///
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>>();
275295
///
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());
280298
///
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
281304
/// [short name]: bevy_utils::get_short_name
282-
/// [`TypeInfo`]: crate::TypeInfo
283-
/// [0]: crate::Reflect
284-
/// [1]: crate::Reflect
305+
/// [crate-level documentation]: crate
285306
pub struct TypeRegistration {
286307
short_name: String,
287308
data: HashMap<TypeId, Box<dyn TypeData>>,
@@ -377,9 +398,19 @@ impl Clone for TypeRegistration {
377398
}
378399
}
379400
}
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.
381411
///
382412
/// [0]: crate::reflect_trait
413+
/// [crate-level documentation]: crate
383414
pub trait TypeData: Downcast + Send + Sync {
384415
fn clone_type_data(&self) -> Box<dyn TypeData>;
385416
}

0 commit comments

Comments
 (0)