vanguard_plugin/
exports.rs

1use crate::VanguardPlugin;
2
3/// Raw plugin instance returned from the dynamic library
4#[repr(C)]
5pub struct RawPlugin {
6    /// The plugin instance
7    pub instance: Box<dyn VanguardPlugin>,
8}
9
10/// The type of the plugin creation function that must be exported by plugins
11pub type CreatePluginFn = unsafe extern "C" fn() -> *mut RawPlugin;
12
13/// The name of the plugin creation function that must be exported by plugins
14pub const CREATE_PLUGIN_SYMBOL: &[u8] = b"create_plugin";
15
16/// Helper macro to export the plugin creation function with the correct ABI
17#[macro_export]
18macro_rules! export_plugin {
19    ($plugin_type:ty) => {
20        #[no_mangle]
21        pub unsafe extern "C" fn create_plugin() -> *mut $crate::exports::RawPlugin {
22            // Create the plugin instance
23            let plugin = <$plugin_type>::default();
24
25            // Box the concrete type
26            let instance = Box::new(plugin) as Box<dyn $crate::VanguardPlugin>;
27
28            // Create the raw plugin
29            let raw = $crate::exports::RawPlugin { instance };
30
31            // Convert to raw pointer
32            Box::into_raw(Box::new(raw))
33        }
34    };
35}
36
37/// Helper macro to export the plugin creation function with a custom constructor
38#[macro_export]
39macro_rules! export_plugin_with {
40    ($plugin_type:ty, $constructor:expr) => {
41        #[no_mangle]
42        pub unsafe extern "C" fn create_plugin() -> *mut $crate::exports::RawPlugin {
43            // Create the plugin instance using the provided constructor
44            let plugin = $constructor();
45
46            // Box the concrete type
47            let instance = Box::new(plugin) as Box<dyn $crate::VanguardPlugin>;
48
49            // Create the raw plugin
50            let raw = $crate::exports::RawPlugin { instance };
51
52            // Convert to raw pointer
53            Box::into_raw(Box::new(raw))
54        }
55    };
56}