Skip to content

Commit 196b4d8

Browse files
committed
Remove try_register
1 parent b464258 commit 196b4d8

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/registration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) fn impl_get_type_registration<'a>(
1717
let type_deps_fn = type_dependencies.map(|deps| {
1818
quote! {
1919
fn register_type_dependencies(registry: &mut #bevy_reflect_path::__macro_exports::TypeRegistry) {
20-
#(registry.try_register::<#deps>();)*
20+
#(registry.register::<#deps>();)*
2121
}
2222
}
2323
});

crates/bevy_reflect/src/impls/std.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ macro_rules! impl_reflect_for_veclike {
312312
}
313313

314314
fn register_type_dependencies(registry: &mut TypeRegistry) {
315-
registry.try_register::<T>();
315+
registry.register::<T>();
316316
}
317317
}
318318

@@ -510,8 +510,8 @@ where
510510
}
511511

512512
fn register_type_dependencies(registry: &mut TypeRegistry) {
513-
registry.try_register::<K>();
514-
registry.try_register::<V>();
513+
registry.register::<K>();
514+
registry.register::<V>();
515515
}
516516
}
517517

@@ -680,7 +680,7 @@ macro_rules! impl_array_get_type_registration {
680680
}
681681

682682
fn register_type_dependencies(registry: &mut TypeRegistry) {
683-
registry.try_register::<T>();
683+
registry.register::<T>();
684684
}
685685
}
686686
)+
@@ -700,7 +700,7 @@ impl<T: FromReflect + GetTypeRegistration> GetTypeRegistration for Option<T> {
700700
}
701701

702702
fn register_type_dependencies(registry: &mut TypeRegistry) {
703-
registry.try_register::<T>();
703+
registry.register::<T>();
704704
}
705705
}
706706

crates/bevy_reflect/src/tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ macro_rules! impl_reflect_tuple {
586586
}
587587

588588
fn register_type_dependencies(_registry: &mut TypeRegistry) {
589-
$(_registry.try_register::<$name>();)*
589+
$(_registry.register::<$name>();)*
590590
}
591591
}
592592

crates/bevy_reflect/src/type_registry.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ pub trait GetTypeRegistration {
3838
///
3939
/// This method is called by [`TypeRegistry::register`] to register any other required types.
4040
/// Often, this is done for fields of structs and enum variants to ensure all types are properly registered.
41-
///
42-
/// If manually implementing, it is _highly_ recommended to use [`TypeRegistry::try_register`] for these dependent types.
43-
/// Using this method allows the type to be skipped if it has already been registered, thus preventing any
44-
/// undesired overwrites and reducing registration costs.
4541
#[allow(unused_variables)]
4642
fn register_type_dependencies(registry: &mut TypeRegistry) {}
4743
}
@@ -84,39 +80,53 @@ impl TypeRegistry {
8480
registry
8581
}
8682

87-
/// Registers the type `T`, adding reflect data as specified in the [`Reflect`] derive:
83+
/// Attempts to register the type `T` if it has not yet been registered already.
84+
///
85+
/// If the registration for type `T` already exists, it will not be registered again.
86+
/// To register the type, overwriting any existing registration, use [register](Self::register) instead.
87+
///
88+
/// Additionally, this will add the reflect [data](TypeData) as specified in the [`Reflect`] derive:
8889
/// ```rust,ignore
8990
/// #[derive(Reflect)]
9091
/// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
92+
/// struct Foo;
9193
/// ```
9294
pub fn register<T>(&mut self)
9395
where
9496
T: GetTypeRegistration,
9597
{
96-
self.add_registration(T::get_type_registration());
98+
if !self.add_registration(T::get_type_registration()) {
99+
return;
100+
}
101+
97102
T::register_type_dependencies(self);
98103
}
99104

100-
/// Attempts to register the type `T` if it has not yet been registered already.
105+
/// Attempts to register the type described by `registration`.
101106
///
102-
/// If the registration for type `T` already exists, it will not be registered again.
107+
/// If the registration for the type already exists, it will not be registered again.
103108
///
104-
/// To register the type, overwriting any existing registration, use [register](Self::register) instead.
105-
pub fn try_register<T>(&mut self)
106-
where
107-
T: GetTypeRegistration + 'static,
108-
{
109-
if !self.contains(TypeId::of::<T>()) {
110-
self.register::<T>();
109+
/// To forcibly register the type, overwriting any existing registration, use the
110+
/// [`force_add_registration`](Self::force_add_registration) method instead.
111+
///
112+
/// Returns `true` if the registration was successfully added,
113+
/// or `false` if it already exists.
114+
pub fn add_registration(&mut self, registration: TypeRegistration) -> bool {
115+
if self.contains(registration.type_id()) {
116+
false
117+
} else {
118+
self.force_add_registration(registration);
119+
true
111120
}
112121
}
113122

114123
/// Registers the type described by `registration`.
115-
pub fn add_registration(&mut self, registration: TypeRegistration) {
116-
if self.registrations.contains_key(&registration.type_id()) {
117-
return;
118-
}
119-
124+
///
125+
/// If the registration for the type already exists, it will be overwritten.
126+
///
127+
/// To avoid overwriting existing registrations, it's recommended to use the
128+
/// [`register`](Self::register) or [`add_registration`](Self::add_registration) methods instead.
129+
pub fn force_add_registration(&mut self, registration: TypeRegistration) {
120130
let short_name = registration.short_name.to_string();
121131
if self.short_name_to_id.contains_key(&short_name)
122132
|| self.ambiguous_names.contains(&short_name)

0 commit comments

Comments
 (0)