Skip to content

Commit 3ac6285

Browse files
committed
Remove try_register
1 parent 88330ad commit 3ac6285

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
@@ -20,7 +20,7 @@ pub(crate) fn impl_get_type_registration<'a>(
2020
let type_deps_fn = type_dependencies.map(|deps| {
2121
quote! {
2222
fn register_type_dependencies(registry: &mut #bevy_reflect_path::__macro_exports::TypeRegistry) {
23-
#(registry.try_register::<#deps>();)*
23+
#(registry.register::<#deps>();)*
2424
}
2525
}
2626
});

crates/bevy_reflect/src/impls/std.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ macro_rules! impl_reflect_for_veclike {
323323
}
324324

325325
fn register_type_dependencies(registry: &mut TypeRegistry) {
326-
registry.try_register::<T>();
326+
registry.register::<T>();
327327
}
328328
}
329329

@@ -519,8 +519,8 @@ where
519519
}
520520

521521
fn register_type_dependencies(registry: &mut TypeRegistry) {
522-
registry.try_register::<K>();
523-
registry.try_register::<V>();
522+
registry.register::<K>();
523+
registry.register::<V>();
524524
}
525525
}
526526

@@ -686,7 +686,7 @@ macro_rules! impl_array_get_type_registration {
686686
}
687687

688688
fn register_type_dependencies(registry: &mut TypeRegistry) {
689-
registry.try_register::<T>();
689+
registry.register::<T>();
690690
}
691691
}
692692
)+
@@ -706,7 +706,7 @@ impl<T: FromReflect + GetTypeRegistration> GetTypeRegistration for Option<T> {
706706
}
707707

708708
fn register_type_dependencies(registry: &mut TypeRegistry) {
709-
registry.try_register::<T>();
709+
registry.register::<T>();
710710
}
711711
}
712712

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)