2020-07-26 19:10:18 +00:00
|
|
|
use crate::AppBuilder;
|
2020-01-21 04:10:40 +00:00
|
|
|
use libloading::{Library, Symbol};
|
|
|
|
use std::any::Any;
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
pub trait Plugin: 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 {
|
2020-05-01 08:02:13 +00:00
|
|
|
std::any::type_name::<Self>()
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
2020-01-21 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
pub type CreatePlugin = unsafe fn() -> *mut dyn Plugin;
|
2020-01-21 04:10:40 +00:00
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
pub fn load_plugin(path: &str) -> (Library, Box<dyn Plugin>) {
|
2020-01-21 04:10:40 +00:00
|
|
|
let lib = Library::new(path).unwrap();
|
|
|
|
|
|
|
|
unsafe {
|
2020-08-08 03:22:17 +00:00
|
|
|
let func: Symbol<CreatePlugin> = lib.get(b"_create_plugin").unwrap();
|
2020-01-21 04:10:40 +00:00
|
|
|
let plugin = Box::from_raw(func());
|
|
|
|
(lib, plugin)
|
|
|
|
}
|
2020-05-01 08:50:07 +00:00
|
|
|
}
|