Add TypeRegistry::register_by_val (#17817)

# Objective

It is impossible to register a type with `TypeRegistry::register` if the
type is unnameable (in the current scope).

## Solution

Add `TypeRegistry::register_by_val` which mirrors std's `size_of_val`
and friends.

## Testing

There's a doc test (unrelated but there seem to be some pre-existing
broken doc links in `bevy_reflect`).
This commit is contained in:
Alexandra 2025-02-15 14:07:01 -05:00 committed by GitHub
parent 3c9e696faa
commit 253cc6a77b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -165,6 +165,43 @@ impl TypeRegistry {
}
}
/// Attempts to register the referenced type `T` if it has not yet been registered.
///
/// See [`register`] for more details.
///
/// # Example
///
/// ```
/// # use bevy_reflect::{Reflect, TypeRegistry};
/// # use core::any::TypeId;
/// #
/// # let mut type_registry = TypeRegistry::default();
/// #
/// #[derive(Reflect)]
/// struct Foo {
/// bar: Bar,
/// }
///
/// #[derive(Reflect)]
/// struct Bar;
///
/// let foo = Foo { bar: Bar };
///
/// // Equivalent to `type_registry.register::<Foo>()`
/// type_registry.register_by_val(&foo);
///
/// assert!(type_registry.contains(TypeId::of::<Foo>()));
/// assert!(type_registry.contains(TypeId::of::<Bar>()));
/// ```
///
/// [`register`]: Self::register
pub fn register_by_val<T>(&mut self, _: &T)
where
T: GetTypeRegistration,
{
self.register::<T>();
}
/// Attempts to register the type described by `registration`.
///
/// If the registration for the type already exists, it will not be registered again.