Skip to content

Instantly share code, notes, and snippets.

@Kilobyte22
Last active August 4, 2017 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kilobyte22/a735ec4ba6cc4d488d42b3449548002b to your computer and use it in GitHub Desktop.
Save Kilobyte22/a735ec4ba6cc4d488d42b3449548002b to your computer and use it in GitHub Desktop.
Proof of Concept Plugin system for rust
extern crate libloading as lib;
extern crate libplugin as plugin;
use plugin::Plugin;
fn main() {
let lib = lib::Library::new("plugins/libtestplugin.so").unwrap();
unsafe {
let func: lib::Symbol<unsafe fn() -> Box<Plugin>> = lib.get(b"gimme_pluginz\0").unwrap();
let plugin: Box<Plugin> = func();
plugin.test();
}
}
pub trait Plugin {
fn test(&self);
}
#[macro_export]
macro_rules! plugin {
($struct:ty) => {
#[no_mangle]
pub fn gimme_pluginz() -> Box<Plugin> {
Box::new($struct::new())
}
}
}
#[macro_use] extern crate libplugin;
use libplugin::Plugin;
struct MyPlugin;
impl MyPlugin {
fn new() -> MyPlugin {
MyPlugin
}
}
impl Plugin for MyPlugin {
fn test(&self) {
println!("test");
}
}
plugin!(MyPlugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment