Skip to content

Commit a2798d7

Browse files
committed
Remove try_register
1 parent a393ffc commit a2798d7

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
@@ -321,7 +321,7 @@ macro_rules! impl_reflect_for_veclike {
321321
}
322322

323323
fn register_type_dependencies(registry: &mut TypeRegistry) {
324-
registry.try_register::<T>();
324+
registry.register::<T>();
325325
}
326326
}
327327

@@ -535,8 +535,8 @@ macro_rules! impl_reflect_for_hashmap {
535535
}
536536

537537
fn register_type_dependencies(registry: &mut TypeRegistry) {
538-
registry.try_register::<K>();
539-
registry.try_register::<V>();
538+
registry.register::<K>();
539+
registry.register::<V>();
540540
}
541541
}
542542

@@ -712,7 +712,7 @@ macro_rules! impl_array_get_type_registration {
712712
}
713713

714714
fn register_type_dependencies(registry: &mut TypeRegistry) {
715-
registry.try_register::<T>();
715+
registry.register::<T>();
716716
}
717717
}
718718
)+
@@ -732,7 +732,7 @@ impl<T: FromReflect + GetTypeRegistration> GetTypeRegistration for Option<T> {
732732
}
733733

734734
fn register_type_dependencies(registry: &mut TypeRegistry) {
735-
registry.try_register::<T>();
735+
registry.register::<T>();
736736
}
737737
}
738738

crates/bevy_reflect/src/tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ macro_rules! impl_reflect_tuple {
590590
}
591591

592592
fn register_type_dependencies(_registry: &mut TypeRegistry) {
593-
$(_registry.try_register::<$name>();)*
593+
$(_registry.register::<$name>();)*
594594
}
595595
}
596596

crates/bevy_reflect/src/type_registry.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ pub trait GetTypeRegistration {
5555
///
5656
/// This method is called by [`TypeRegistry::register`] to register any other required types.
5757
/// Often, this is done for fields of structs and enum variants to ensure all types are properly registered.
58-
///
59-
/// If manually implementing, it is _highly_ recommended to use [`TypeRegistry::try_register`] for these dependent types.
60-
/// Using this method allows the type to be skipped if it has already been registered, thus preventing any
61-
/// undesired overwrites and reducing registration costs.
6258
#[allow(unused_variables)]
6359
fn register_type_dependencies(registry: &mut TypeRegistry) {}
6460
}
@@ -103,39 +99,53 @@ impl TypeRegistry {
10399
registry
104100
}
105101

106-
/// Registers the type `T`, adding reflect data as specified in the [`Reflect`] derive:
102+
/// Attempts to register the type `T` if it has not yet been registered already.
103+
///
104+
/// If the registration for type `T` already exists, it will not be registered again.
105+
/// To register the type, overwriting any existing registration, use [register](Self::register) instead.
106+
///
107+
/// Additionally, this will add the reflect [data](TypeData) as specified in the [`Reflect`] derive:
107108
/// ```rust,ignore
108109
/// #[derive(Reflect)]
109110
/// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
111+
/// struct Foo;
110112
/// ```
111113
pub fn register<T>(&mut self)
112114
where
113115
T: GetTypeRegistration,
114116
{
115-
self.add_registration(T::get_type_registration());
117+
if !self.add_registration(T::get_type_registration()) {
118+
return;
119+
}
120+
116121
T::register_type_dependencies(self);
117122
}
118123

119-
/// Attempts to register the type `T` if it has not yet been registered already.
124+
/// Attempts to register the type described by `registration`.
120125
///
121-
/// If the registration for type `T` already exists, it will not be registered again.
126+
/// If the registration for the type already exists, it will not be registered again.
122127
///
123-
/// To register the type, overwriting any existing registration, use [register](Self::register) instead.
124-
pub fn try_register<T>(&mut self)
125-
where
126-
T: GetTypeRegistration + 'static,
127-
{
128-
if !self.contains(TypeId::of::<T>()) {
129-
self.register::<T>();
128+
/// To forcibly register the type, overwriting any existing registration, use the
129+
/// [`force_add_registration`](Self::force_add_registration) method instead.
130+
///
131+
/// Returns `true` if the registration was successfully added,
132+
/// or `false` if it already exists.
133+
pub fn add_registration(&mut self, registration: TypeRegistration) -> bool {
134+
if self.contains(registration.type_id()) {
135+
false
136+
} else {
137+
self.force_add_registration(registration);
138+
true
130139
}
131140
}
132141

133142
/// Registers the type described by `registration`.
134-
pub fn add_registration(&mut self, registration: TypeRegistration) {
135-
if self.registrations.contains_key(&registration.type_id()) {
136-
return;
137-
}
138-
143+
///
144+
/// If the registration for the type already exists, it will be overwritten.
145+
///
146+
/// To avoid overwriting existing registrations, it's recommended to use the
147+
/// [`register`](Self::register) or [`add_registration`](Self::add_registration) methods instead.
148+
pub fn force_add_registration(&mut self, registration: TypeRegistration) {
139149
let short_name = registration.short_name.to_string();
140150
if self.short_name_to_id.contains_key(&short_name)
141151
|| self.ambiguous_names.contains(&short_name)

0 commit comments

Comments
 (0)