Skip to content

Commit 76881d7

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

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

crates/bevy_app/src/plugin_group.rs

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct PluginEntry {
1111
enabled: bool,
1212
}
1313

14-
/// Builds and customizes a plugin group. A plugin group is an ordered list of plugins that
14+
/// Builds and customizes a plugin group. A plugin group is an ordered list of plugins
1515
/// that can be enabled, disabled or reordered.
1616
#[derive(Default)]
1717
pub struct PluginGroupBuilder {
@@ -20,24 +20,9 @@ pub struct PluginGroupBuilder {
2020
}
2121

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

51+
/// Adds the plugin `plugin` at the end of this `PluginGroupBuilder`. If the plugin was
52+
/// already in the group, it is removed from its previous place.
53+
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
54+
let target_index = self.order.len();
55+
self.order.push(TypeId::of::<T>());
56+
self.upsert_plugin_state(plugin, target_index);
5757
self
5858
}
5959

@@ -74,21 +74,7 @@ impl PluginGroupBuilder {
7474
)
7575
});
7676
self.order.insert(target_index, TypeId::of::<T>());
77-
if let Some(entry) = self.plugins.insert(
78-
TypeId::of::<T>(),
79-
PluginEntry {
80-
plugin: Box::new(plugin),
81-
enabled: true,
82-
},
83-
) {
84-
if entry.enabled {
85-
warn!(
86-
"You are replacing plugin '{}' that was not disabled.",
87-
entry.plugin.name()
88-
);
89-
}
90-
self.remove_when_adding::<T>(target_index);
91-
}
77+
self.upsert_plugin_state(plugin, target_index);
9278
self
9379
}
9480

@@ -110,21 +96,7 @@ impl PluginGroupBuilder {
11096
})
11197
+ 1;
11298
self.order.insert(target_index, TypeId::of::<T>());
113-
if let Some(entry) = self.plugins.insert(
114-
TypeId::of::<T>(),
115-
PluginEntry {
116-
plugin: Box::new(plugin),
117-
enabled: true,
118-
},
119-
) {
120-
if entry.enabled {
121-
warn!(
122-
"You are replacing plugin '{}' that was not disabled.",
123-
entry.plugin.name()
124-
);
125-
}
126-
self.remove_when_adding::<T>(target_index);
127-
}
99+
self.upsert_plugin_state(plugin, target_index);
128100
self
129101
}
130102

0 commit comments

Comments
 (0)