Skip to content

Commit

Permalink
make register on TypeRegistry idempotent (#6487)
Browse files Browse the repository at this point in the history
# Objective

- adding a new `.register` should not overwrite old type data
- separate crates should both be able to register the same type

I ran into this while debugging why `register::<Handle<T>>` removed the `ReflectHandle` type data from a prior `register_asset_reflect`.


## Solution

- make `register` do nothing if called again for the same type
- I also removed some unnecessary duplicate registrations
  • Loading branch information
jakobhellermann committed Nov 5, 2022
1 parent 3d64acd commit 5ae9475
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 2 additions & 5 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ impl Plugin for CorePlugin {
);

app.register_type::<Entity>().register_type::<Name>();
app.register_type::<Entity>()
.register_type::<Name>()
.register_type::<Range<f32>>()
.register_type_data::<Range<f32>, ReflectSerialize>()
.register_type_data::<Range<f32>, ReflectDeserialize>();

register_rust_types(app);
register_math_types(app);
Expand All @@ -63,6 +58,8 @@ impl Plugin for CorePlugin {

fn register_rust_types(app: &mut App) {
app.register_type::<Range<f32>>()
.register_type_data::<Range<f32>, ReflectSerialize>()
.register_type_data::<Range<f32>, ReflectDeserialize>()
.register_type::<String>()
.register_type::<HashSet<String>>()
.register_type::<Option<String>>()
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl TypeRegistry {

/// Registers the type described by `registration`.
pub fn add_registration(&mut self, registration: TypeRegistration) {
if self.registrations.contains_key(&registration.type_id()) {
return;
}

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 5ae9475

Please sign in to comment.