bevyengine / bevyengine/bevy

Implement `Plugin` for `Box<dyn Plugin>`

Open
#9,700 1 comment 2 reactions 0 assignees View on GitHub
A-App C-Usability D-Trivial X-Contentious
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

`DynamicPlugin`s cannot currently be added directly to an `App`, since they are passed across shared library boundaries as `Box`, which does not implement `Plugin`. In short, a simple use case of `bevy_dynamic_plugin` like this should work (ignoring the library's lifetime for demo purposes):

```rust
use bevy::prelude::*;

struct MyModLoaderPlugin{
mod_library: PathBuf,
}

impl MyModLoaderPlugin {
fn build(&self, app: &mut App) {
let (library, plugin) = unsafe { bevy_dynamic_plugin::dynamically_load_plugin(&self.mod_library) }?;
app.add_plugins(plugin);
// Store the library somewhere permanent so it doesn't get unloaded...
}
}
```

## What solution would you like?

Other than some intricacies unknown to me, this should work:

```rust
impl Plugin for Box {
fn build(&self, app: &mut App) {
self.as_ref().build(app);
}

fn ready(&self, app: &App) -> bool {
self.as_ref().ready(app)
}

fn finish(&self, app: &mut App) {
self.as_ref().finish(app);
}

fn cleanup(&self, app: &mut App) {
self.as_ref().cleanup(app);
}

fn name(&self) -> &str {
self.as_ref().name()
}

fn is_unique(&self) -> bool {
self.as_ref().is_unique()
}
}
```

## What alternative(s) have you considered?

I am currently using a wrapper struct to effectively provide the above implementation while respecting trait implementation orphaning rules. It works, but it's unnecessary boilerplate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.