Last active
August 4, 2017 22:11
-
-
Save Kilobyte22/a735ec4ba6cc4d488d42b3449548002b to your computer and use it in GitHub Desktop.
Proof of Concept Plugin system for rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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