Skip to content

Commit

Permalink
Adapt path type of dynamically_load_plugin (bevyengine#6734)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#6711

## Solution

- Change the `path` function parameter of `dynamically_load_plugin` and `DynamicPluginExt::load_plugin` to a generic with `AsRef<OsStr>` bound
  • Loading branch information
polygon authored and ickshonpe committed Dec 6, 2022
1 parent d67e555 commit 14b4d3f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions crates/bevy_dynamic_plugin/src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use libloading::{Library, Symbol};
use std::ffi::OsStr;
use thiserror::Error;

use bevy_app::{App, CreatePlugin, Plugin};
Expand All @@ -20,8 +21,8 @@ pub enum DynamicPluginLoadError {
/// The specified plugin must be linked against the exact same libbevy.so as this program.
/// In addition the `_bevy_create_plugin` symbol must not be manually created, but instead created
/// by deriving `DynamicPlugin` on a unit struct implementing [`Plugin`].
pub unsafe fn dynamically_load_plugin(
path: &str,
pub unsafe fn dynamically_load_plugin<P: AsRef<OsStr>>(
path: P,
) -> Result<(Library, Box<dyn Plugin>), DynamicPluginLoadError> {
let lib = Library::new(path).map_err(DynamicPluginLoadError::Library)?;
let func: Symbol<CreatePlugin> = lib
Expand All @@ -35,11 +36,11 @@ pub trait DynamicPluginExt {
/// # Safety
///
/// Same as [`dynamically_load_plugin`].
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self;
unsafe fn load_plugin<P: AsRef<OsStr>>(&mut self, path: P) -> &mut Self;
}

impl DynamicPluginExt for App {
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self {
unsafe fn load_plugin<P: AsRef<OsStr>>(&mut self, path: P) -> &mut Self {
let (lib, plugin) = dynamically_load_plugin(path).unwrap();
std::mem::forget(lib); // Ensure that the library is not automatically unloaded
plugin.build(self);
Expand Down

0 comments on commit 14b4d3f

Please sign in to comment.