diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs b/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs index 20a3f56474aff5..dcf1ba05c2c46e 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs @@ -17,7 +17,7 @@ pub(crate) fn impl_get_type_registration<'a>( let type_deps_fn = type_dependencies.map(|deps| { quote! { fn register_type_dependencies(registry: &mut #bevy_reflect_path::__macro_exports::TypeRegistry) { - #(registry.try_register::<#deps>();)* + #(registry.register::<#deps>();)* } } }); diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 03fb526cb1f878..096bd262c19687 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -312,7 +312,7 @@ macro_rules! impl_reflect_for_veclike { } fn register_type_dependencies(registry: &mut TypeRegistry) { - registry.try_register::(); + registry.register::(); } } @@ -510,8 +510,8 @@ where } fn register_type_dependencies(registry: &mut TypeRegistry) { - registry.try_register::(); - registry.try_register::(); + registry.register::(); + registry.register::(); } } @@ -680,7 +680,7 @@ macro_rules! impl_array_get_type_registration { } fn register_type_dependencies(registry: &mut TypeRegistry) { - registry.try_register::(); + registry.register::(); } } )+ @@ -700,7 +700,7 @@ impl GetTypeRegistration for Option { } fn register_type_dependencies(registry: &mut TypeRegistry) { - registry.try_register::(); + registry.register::(); } } diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 34ae11d2690478..80d156eeccc065 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -586,7 +586,7 @@ macro_rules! impl_reflect_tuple { } fn register_type_dependencies(_registry: &mut TypeRegistry) { - $(_registry.try_register::<$name>();)* + $(_registry.register::<$name>();)* } } diff --git a/crates/bevy_reflect/src/type_registry.rs b/crates/bevy_reflect/src/type_registry.rs index 01189600dd1982..f4b86625d244fe 100644 --- a/crates/bevy_reflect/src/type_registry.rs +++ b/crates/bevy_reflect/src/type_registry.rs @@ -38,10 +38,6 @@ pub trait GetTypeRegistration { /// /// This method is called by [`TypeRegistry::register`] to register any other required types. /// Often, this is done for fields of structs and enum variants to ensure all types are properly registered. - /// - /// If manually implementing, it is _highly_ recommended to use [`TypeRegistry::try_register`] for these dependent types. - /// Using this method allows the type to be skipped if it has already been registered, thus preventing any - /// undesired overwrites and reducing registration costs. #[allow(unused_variables)] fn register_type_dependencies(registry: &mut TypeRegistry) {} } @@ -84,39 +80,53 @@ impl TypeRegistry { registry } - /// Registers the type `T`, adding reflect data as specified in the [`Reflect`] derive: + /// Attempts to register the type `T` if it has not yet been registered already. + /// + /// If the registration for type `T` already exists, it will not be registered again. + /// To register the type, overwriting any existing registration, use [register](Self::register) instead. + /// + /// Additionally, this will add the reflect [data](TypeData) as specified in the [`Reflect`] derive: /// ```rust,ignore /// #[derive(Reflect)] /// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize + /// struct Foo; /// ``` pub fn register(&mut self) where T: GetTypeRegistration, { - self.add_registration(T::get_type_registration()); + if !self.add_registration(T::get_type_registration()) { + return; + } + T::register_type_dependencies(self); } - /// Attempts to register the type `T` if it has not yet been registered already. + /// Attempts to register the type described by `registration`. /// - /// If the registration for type `T` already exists, it will not be registered again. + /// If the registration for the type already exists, it will not be registered again. /// - /// To register the type, overwriting any existing registration, use [register](Self::register) instead. - pub fn try_register(&mut self) - where - T: GetTypeRegistration + 'static, - { - if !self.contains(TypeId::of::()) { - self.register::(); + /// To forcibly register the type, overwriting any existing registration, use the + /// [`force_add_registration`](Self::force_add_registration) method instead. + /// + /// Returns `true` if the registration was successfully added, + /// or `false` if it already exists. + pub fn add_registration(&mut self, registration: TypeRegistration) -> bool { + if self.contains(registration.type_id()) { + false + } else { + self.force_add_registration(registration); + true } } /// Registers the type described by `registration`. - pub fn add_registration(&mut self, registration: TypeRegistration) { - if self.registrations.contains_key(®istration.type_id()) { - return; - } - + /// + /// If the registration for the type already exists, it will be overwritten. + /// + /// To avoid overwriting existing registrations, it's recommended to use the + /// [`register`](Self::register) or [`add_registration`](Self::add_registration) methods instead. + pub fn force_add_registration(&mut self, registration: TypeRegistration) { let short_name = registration.short_name.to_string(); if self.short_name_to_id.contains_key(&short_name) || self.ambiguous_names.contains(&short_name)