Skip to content

Commit

Permalink
Remove try_register
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Nov 10, 2022
1 parent 162c3d0 commit 79fa0d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>();)*
}
}
});
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<T: FromReflect + GetTypeRegistration> GetTypeRegistration for Vec<T> {
}

fn register_type_dependencies(registry: &mut TypeRegistry) {
registry.try_register::<T>();
registry.register::<T>();
}
}

Expand Down Expand Up @@ -483,8 +483,8 @@ where
}

fn register_type_dependencies(registry: &mut TypeRegistry) {
registry.try_register::<K>();
registry.try_register::<V>();
registry.register::<K>();
registry.register::<V>();
}
}

Expand Down Expand Up @@ -653,7 +653,7 @@ macro_rules! impl_array_get_type_registration {
}

fn register_type_dependencies(registry: &mut TypeRegistry) {
registry.try_register::<T>();
registry.register::<T>();
}
}
)+
Expand Down Expand Up @@ -753,7 +753,7 @@ impl<T: FromReflect + GetTypeRegistration> GetTypeRegistration for Option<T> {
}

fn register_type_dependencies(registry: &mut TypeRegistry) {
registry.try_register::<T>();
registry.register::<T>();
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ macro_rules! impl_reflect_tuple {
}

fn register_type_dependencies(_registry: &mut TypeRegistry) {
$(_registry.try_register::<$name>();)*
$(_registry.register::<$name>();)*
}
}

Expand Down
50 changes: 30 additions & 20 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
Expand Down Expand Up @@ -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<T>(&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<T>(&mut self)
where
T: GetTypeRegistration + 'static,
{
if !self.contains(TypeId::of::<T>()) {
self.register::<T>();
/// 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(&registration.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)
Expand Down

0 comments on commit 79fa0d6

Please sign in to comment.