Skip to content

Commit

Permalink
Remove second generic from .add_before, .add_after (bevyengine#14285
Browse files Browse the repository at this point in the history
)

# Objective

```rust
// Currently:
builder.add_after::<FooPlugin, _>(BarPlugin);
// After this PR:
builder.add_after::<FooPlugin>(BarPlugin);
```

This removes some weirdness and better parallels the rest of the
`PluginGroupBuilder` API.

## Solution

Define a helper method `type_id_of_val` to use in `.add_before` and
`.add_after` instead of `TypeId::of::<T>` (which requires the plugin
type to be nameable, preventing `impl Plugin` from being used).

## Testing

Ran `cargo run -p ci lints` successfully.

## Migration Guide

Removed second generic from `PluginGroupBuilder` methods: `add_before`
and `add_after`.

```rust
// Before:
DefaultPlugins
    .build()
    .add_before::<WindowPlugin, _>(FooPlugin)
    .add_after::<WindowPlugin, _>(BarPlugin)

// After:
DefaultPlugins
    .build()
    .add_before::<WindowPlugin>(FooPlugin)
    .add_after::<WindowPlugin>(BarPlugin)
```

---------

Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
  • Loading branch information
benfrankel and BD103 authored Jul 15, 2024
1 parent 65aae92 commit 7cb9785
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ impl PluginGroup for PluginGroupBuilder {
}
}

/// Helper method to get the [`TypeId`] of a value without having to name its type.
fn type_id_of_val<T: 'static>(_: &T) -> TypeId {
TypeId::of::<T>()
}

/// Facilitates the creation and configuration of a [`PluginGroup`].
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a [`Resource`](bevy_ecs::system::Resource)
/// are built before/after dependent/depending [`Plugin`]s. [`Plugin`]s inside the group
Expand Down Expand Up @@ -153,19 +158,19 @@ impl PluginGroupBuilder {
/// Adds a [`Plugin`] in this [`PluginGroupBuilder`] before the plugin of type `Target`.
/// If the plugin was already the group, it is removed from its previous place. There must
/// be a plugin of type `Target` in the group or it will panic.
pub fn add_before<Target: Plugin, T: Plugin>(mut self, plugin: T) -> Self {
pub fn add_before<Target: Plugin>(mut self, plugin: impl Plugin) -> Self {
let target_index = self.index_of::<Target>();
self.order.insert(target_index, TypeId::of::<T>());
self.order.insert(target_index, type_id_of_val(&plugin));
self.upsert_plugin_state(plugin, target_index);
self
}

/// Adds a [`Plugin`] in this [`PluginGroupBuilder`] after the plugin of type `Target`.
/// If the plugin was already the group, it is removed from its previous place. There must
/// be a plugin of type `Target` in the group or it will panic.
pub fn add_after<Target: Plugin, T: Plugin>(mut self, plugin: T) -> Self {
pub fn add_after<Target: Plugin>(mut self, plugin: impl Plugin) -> Self {
let target_index = self.index_of::<Target>() + 1;
self.order.insert(target_index, TypeId::of::<T>());
self.order.insert(target_index, type_id_of_val(&plugin));
self.upsert_plugin_state(plugin, target_index);
self
}
Expand Down Expand Up @@ -285,7 +290,7 @@ mod tests {
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add_after::<PluginA, PluginC>(PluginC);
.add_after::<PluginA>(PluginC);

assert_eq!(
group.order,
Expand All @@ -302,7 +307,7 @@ mod tests {
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add_before::<PluginB, PluginC>(PluginC);
.add_before::<PluginB>(PluginC);

assert_eq!(
group.order,
Expand Down Expand Up @@ -338,7 +343,7 @@ mod tests {
.add(PluginA)
.add(PluginB)
.add(PluginC)
.add_after::<PluginA, PluginC>(PluginC);
.add_after::<PluginA>(PluginC);

assert_eq!(
group.order,
Expand All @@ -356,7 +361,7 @@ mod tests {
.add(PluginA)
.add(PluginB)
.add(PluginC)
.add_before::<PluginB, PluginC>(PluginC);
.add_before::<PluginB>(PluginC);

assert_eq!(
group.order,
Expand Down
2 changes: 1 addition & 1 deletion examples/app/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// HelloWorldPlugins
// .build()
// .disable::<PrintWorldPlugin>()
// .add_before::<PrintHelloPlugin, _>(
// .add_before::<PrintHelloPlugin>(
// bevy::diagnostic::LogDiagnosticsPlugin::default(),
// ),
// )
Expand Down

0 comments on commit 7cb9785

Please sign in to comment.