Skip to content

Commit bfde7cc

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

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
@@ -40,24 +40,9 @@ impl PluginGroupBuilder {
4040
}
4141
}
4242

43-
// Removes a previous ordering of a plugin that has just been added at `added_at` index
44-
fn remove_when_adding<T: Plugin>(&mut self, added_at: usize) {
45-
if let Some(to_remove) = self
46-
.order
47-
.iter()
48-
.enumerate()
49-
.find(|(i, ty)| *i != added_at && **ty == TypeId::of::<T>())
50-
.map(|(i, _)| i)
51-
{
52-
self.order.remove(to_remove);
53-
}
54-
}
55-
56-
/// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
57-
/// already in the group, it is removed from its previous place.
58-
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
59-
let target_index = self.order.len();
60-
self.order.push(TypeId::of::<T>());
43+
// Insert the new plugin as enabled, and removes its previous ordering if it was
44+
// already present
45+
fn upsert_plugin_state<T: Plugin>(&mut self, plugin: T, added_at_index: usize) {
6146
if let Some(entry) = self.plugins.insert(
6247
TypeId::of::<T>(),
6348
PluginEntry {
@@ -71,9 +56,24 @@ impl PluginGroupBuilder {
7156
entry.plugin.name()
7257
);
7358
}
74-
self.remove_when_adding::<T>(target_index);
59+
if let Some(to_remove) = self
60+
.order
61+
.iter()
62+
.enumerate()
63+
.find(|(i, ty)| *i != added_at_index && **ty == TypeId::of::<T>())
64+
.map(|(i, _)| i)
65+
{
66+
self.order.remove(to_remove);
67+
}
7568
}
69+
}
7670

71+
/// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
72+
/// already in the group, it is removed from its previous place.
73+
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
74+
let target_index = self.order.len();
75+
self.order.push(TypeId::of::<T>());
76+
self.upsert_plugin_state(plugin, target_index);
7777
self
7878
}
7979

@@ -83,21 +83,7 @@ impl PluginGroupBuilder {
8383
pub fn add_before<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
8484
let target_index = self.index_of::<Target>();
8585
self.order.insert(target_index, TypeId::of::<T>());
86-
if let Some(entry) = self.plugins.insert(
87-
TypeId::of::<T>(),
88-
PluginEntry {
89-
plugin: Box::new(plugin),
90-
enabled: true,
91-
},
92-
) {
93-
if entry.enabled {
94-
warn!(
95-
"You are replacing plugin '{}' that was not disabled.",
96-
entry.plugin.name()
97-
);
98-
}
99-
self.remove_when_adding::<T>(target_index);
100-
}
86+
self.upsert_plugin_state(plugin, target_index);
10187
self
10288
}
10389

@@ -107,21 +93,7 @@ impl PluginGroupBuilder {
10793
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
10894
let target_index = self.index_of::<Target>() + 1;
10995
self.order.insert(target_index, TypeId::of::<T>());
110-
if let Some(entry) = self.plugins.insert(
111-
TypeId::of::<T>(),
112-
PluginEntry {
113-
plugin: Box::new(plugin),
114-
enabled: true,
115-
},
116-
) {
117-
if entry.enabled {
118-
warn!(
119-
"You are replacing plugin '{}' that was not disabled.",
120-
entry.plugin.name()
121-
);
122-
}
123-
self.remove_when_adding::<T>(target_index);
124-
}
96+
self.upsert_plugin_state(plugin, target_index);
12597
self
12698
}
12799

0 commit comments

Comments
 (0)