Skip to content

Commit 91406d8

Browse files
mockersfNiklasEi
andcommitted
factor shared part
Co-Authored-By: Niklas Eicker <git@nikl.me>
1 parent 5b47947 commit 91406d8

File tree

1 file changed

+21
-49
lines changed

1 file changed

+21
-49
lines changed

crates/bevy_app/src/plugin_group.rs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,9 @@ pub struct PluginGroupBuilder {
2424
}
2525

2626
impl PluginGroupBuilder {
27-
// Removes a previous ordering of a plugin that has just been added at `added_at` index
28-
fn remove_when_adding<T: Plugin>(&mut self, added_at: usize) {
29-
if let Some(to_remove) = self
30-
.order
31-
.iter()
32-
.enumerate()
33-
.find(|(i, ty)| *i != added_at && **ty == TypeId::of::<T>())
34-
.map(|(i, _)| i)
35-
{
36-
self.order.remove(to_remove);
37-
}
38-
}
39-
40-
/// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
41-
/// already in the group, it is removed from its previous place.
42-
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
43-
let target_index = self.order.len();
44-
self.order.push(TypeId::of::<T>());
27+
// Insert the new plugin as enabled, and removes its previous ordering if it was
28+
// already present
29+
fn upsert_plugin_state<T: Plugin>(&mut self, plugin: T, added_at_index: usize) {
4530
if let Some(entry) = self.plugins.insert(
4631
TypeId::of::<T>(),
4732
PluginEntry {
@@ -55,9 +40,24 @@ impl PluginGroupBuilder {
5540
entry.plugin.name()
5641
);
5742
}
58-
self.remove_when_adding::<T>(target_index);
43+
if let Some(to_remove) = self
44+
.order
45+
.iter()
46+
.enumerate()
47+
.find(|(i, ty)| *i != added_at_index && **ty == TypeId::of::<T>())
48+
.map(|(i, _)| i)
49+
{
50+
self.order.remove(to_remove);
51+
}
5952
}
53+
}
6054

55+
/// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
56+
/// already in the group, it is removed from its previous place.
57+
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
58+
let target_index = self.order.len();
59+
self.order.push(TypeId::of::<T>());
60+
self.upsert_plugin_state(plugin, target_index);
6161
self
6262
}
6363

@@ -78,21 +78,7 @@ impl PluginGroupBuilder {
7878
)
7979
});
8080
self.order.insert(target_index, TypeId::of::<T>());
81-
if let Some(entry) = self.plugins.insert(
82-
TypeId::of::<T>(),
83-
PluginEntry {
84-
plugin: Box::new(plugin),
85-
enabled: true,
86-
},
87-
) {
88-
if entry.enabled {
89-
warn!(
90-
"You are replacing plugin '{}' that was not disabled.",
91-
entry.plugin.name()
92-
);
93-
}
94-
self.remove_when_adding::<T>(target_index);
95-
}
81+
self.upsert_plugin_state(plugin, target_index);
9682
self
9783
}
9884

@@ -114,21 +100,7 @@ impl PluginGroupBuilder {
114100
})
115101
+ 1;
116102
self.order.insert(target_index, TypeId::of::<T>());
117-
if let Some(entry) = self.plugins.insert(
118-
TypeId::of::<T>(),
119-
PluginEntry {
120-
plugin: Box::new(plugin),
121-
enabled: true,
122-
},
123-
) {
124-
if entry.enabled {
125-
warn!(
126-
"You are replacing plugin '{}' that was not disabled.",
127-
entry.plugin.name()
128-
);
129-
}
130-
self.remove_when_adding::<T>(target_index);
131-
}
103+
self.upsert_plugin_state(plugin, target_index);
132104
self
133105
}
134106

0 commit comments

Comments
 (0)