vanguard-plugin 0.1.1

Plugin system for the Vanguard version manager
Documentation
use crate::VanguardPlugin;

/// Raw plugin instance returned from the dynamic library
#[repr(C)]
pub struct RawPlugin {
    /// The plugin instance
    pub instance: Box<dyn VanguardPlugin>,
}

/// The type of the plugin creation function that must be exported by plugins
pub type CreatePluginFn = unsafe extern "C" fn() -> *mut RawPlugin;

/// The name of the plugin creation function that must be exported by plugins
pub const CREATE_PLUGIN_SYMBOL: &[u8] = b"create_plugin";

/// Helper macro to export the plugin creation function with the correct ABI
#[macro_export]
macro_rules! export_plugin {
    ($plugin_type:ty) => {
        #[no_mangle]
        pub unsafe extern "C" fn create_plugin() -> *mut $crate::exports::RawPlugin {
            // Create the plugin instance
            let plugin = <$plugin_type>::default();

            // Box the concrete type
            let instance = Box::new(plugin) as Box<dyn $crate::VanguardPlugin>;

            // Create the raw plugin
            let raw = $crate::exports::RawPlugin { instance };

            // Convert to raw pointer
            Box::into_raw(Box::new(raw))
        }
    };
}

/// Helper macro to export the plugin creation function with a custom constructor
#[macro_export]
macro_rules! export_plugin_with {
    ($plugin_type:ty, $constructor:expr) => {
        #[no_mangle]
        pub unsafe extern "C" fn create_plugin() -> *mut $crate::exports::RawPlugin {
            // Create the plugin instance using the provided constructor
            let plugin = $constructor();

            // Box the concrete type
            let instance = Box::new(plugin) as Box<dyn $crate::VanguardPlugin>;

            // Create the raw plugin
            let raw = $crate::exports::RawPlugin { instance };

            // Convert to raw pointer
            Box::into_raw(Box::new(raw))
        }
    };
}