plugrs_host/
plugin_loader.rs1use libloading::{Library, Symbol};
2use plugrs_interface::Plugin;
3use std::ffi::OsStr;
4
5pub struct PluginLoader {
6 lib: Library,
7}
8
9impl PluginLoader {
10 pub fn new<P: AsRef<OsStr>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
11 let lib = unsafe { Library::new(path)? };
12 Ok(Self { lib })
13 }
14
15 pub fn load_plugin(&self) -> Result<Box<dyn Plugin>, Box<dyn std::error::Error>> {
16 let creator: Symbol<fn() -> Box<dyn Plugin>> = unsafe { self.lib.get(b"create_plugin")? };
17 Ok(creator())
18 }
19}