pluginator

Crates.iopluginator
lib.rspluginator
version1.0.1
sourcesrc
created_at2023-03-23 01:57:56.267107
updated_at2023-03-23 02:03:19.301196
descriptionUtilities for easier creation of plugins in Rust
homepage
repository
max_upload_size
id817674
size4,506
(megahomyak)

documentation

README

Usage

Use plugin_trait! and plugin_implementation! in different crates.

Wrap the plugin trait with the plugin_trait! macro. A load_plugin function will be generated. It can load a plugin object from a static library.

Wrap the plugin implementation with a plugin_implementation! macro. A get_interface function will be generated. Compile the crate as a static library to use the plugin.

Safety

load_plugin is unsafe because you should pass the correct (matching) plugin trait as its first argument, otherwise undefined behavior will happen. This happens because it is impossible to determine the return value type of an interface getter in a static library.

Example

  • app/src/main.rs:
fn main() {
    let plugin = unsafe { app::load_plugin("plugins/libplugin1.so") }.unwrap();
    plugin.print("hello");
}
  • app/src/lib.rs:
pub trait Plugin: Sync + Send {
    fn print(&self, message: &str);
}

pluginator::plugin_trait!(Plugin);
  • plugin1/src/lib.rs:
struct Plugin;

impl app::Plugin for Plugin {
    fn print(&self, message: &str) {
        println!("{}", message);
    }
}

pluginator::plugin_implementation!(app::Plugin, Plugin);
Commit count: 0

cargo fmt