2020-04-06 03:19:02 +00:00
|
|
|
use super::AppBuilder;
|
2020-01-21 04:10:40 +00:00
|
|
|
use libloading::{Library, Symbol};
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
pub trait AppPlugin: Any + Send + Sync {
|
2020-04-06 03:19:02 +00:00
|
|
|
fn build(&self, app: &mut AppBuilder);
|
2020-04-05 21:12:14 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
type_name_of_val(self)
|
|
|
|
}
|
2020-01-21 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 08:04:27 +00:00
|
|
|
pub type CreateAppPlugin = unsafe fn() -> *mut dyn AppPlugin;
|
2020-01-21 04:10:40 +00:00
|
|
|
|
|
|
|
pub fn load_plugin(path: &str) -> (Library, Box<dyn AppPlugin>) {
|
|
|
|
let lib = Library::new(path).unwrap();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let func: Symbol<CreateAppPlugin> = lib.get(b"_create_plugin").unwrap();
|
|
|
|
let plugin = Box::from_raw(func());
|
|
|
|
(lib, plugin)
|
|
|
|
}
|
2020-02-08 07:17:51 +00:00
|
|
|
}
|
2020-04-05 21:12:14 +00:00
|
|
|
|
|
|
|
fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
|
|
|
|
std::any::type_name::<T>()
|
|
|
|
}
|