Skip to content

Commit b123390

Browse files
its-justusexjam
authored andcommitted
Tidy up PluginGroupBuilder by moving Plugin index retrieval to it's own function (bevyengine#4446)
# Objective - Clean up duplicate code in the add_before/add_after functions in PluginGroupBuilder. ## Solution - moved index retrieval code to a private function index_of() for the PluginGroupBuilder. - change is just tidying up. No real change to functionality.
1 parent a7bea59 commit b123390

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

crates/bevy_app/src/plugin_group.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ pub struct PluginGroupBuilder {
2323
}
2424

2525
impl PluginGroupBuilder {
26+
/// Finds the index of a target [`Plugin`]. Panics if the target's [`TypeId`] is not found.
27+
fn index_of<Target: Plugin>(&mut self) -> usize {
28+
let index = self
29+
.order
30+
.iter()
31+
.position(|&ty| ty == TypeId::of::<Target>());
32+
33+
match index {
34+
Some(i) => i,
35+
None => panic!(
36+
"Plugin does not exist in group: {}.",
37+
std::any::type_name::<Target>()
38+
),
39+
}
40+
}
41+
2642
/// Appends a [`Plugin`] to the [`PluginGroupBuilder`].
2743
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
2844
self.order.push(TypeId::of::<T>());
@@ -38,18 +54,7 @@ impl PluginGroupBuilder {
3854

3955
/// Configures a [`Plugin`] to be built before another plugin.
4056
pub fn add_before<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
41-
let target_index = self
42-
.order
43-
.iter()
44-
.enumerate()
45-
.find(|(_i, ty)| **ty == TypeId::of::<Target>())
46-
.map(|(i, _)| i)
47-
.unwrap_or_else(|| {
48-
panic!(
49-
"Plugin does not exist: {}.",
50-
std::any::type_name::<Target>()
51-
)
52-
});
57+
let target_index = self.index_of::<Target>();
5358
self.order.insert(target_index, TypeId::of::<T>());
5459
self.plugins.insert(
5560
TypeId::of::<T>(),
@@ -63,18 +68,7 @@ impl PluginGroupBuilder {
6368

6469
/// Configures a [`Plugin`] to be built after another plugin.
6570
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
66-
let target_index = self
67-
.order
68-
.iter()
69-
.enumerate()
70-
.find(|(_i, ty)| **ty == TypeId::of::<Target>())
71-
.map(|(i, _)| i)
72-
.unwrap_or_else(|| {
73-
panic!(
74-
"Plugin does not exist: {}.",
75-
std::any::type_name::<Target>()
76-
)
77-
});
71+
let target_index = self.index_of::<Target>();
7872
self.order.insert(target_index + 1, TypeId::of::<T>());
7973
self.plugins.insert(
8074
TypeId::of::<T>(),

0 commit comments

Comments
 (0)