Skip to main content

rustbridge_ffi/
lib.rs

1//! rustbridge-ffi - C ABI exports and FFI buffer management
2//!
3//! This crate provides the FFI boundary layer:
4//! - [`FfiBuffer`] for passing data across FFI
5//! - [`PluginHandle`] for managing plugin instances
6//! - C ABI exported functions (plugin_init, plugin_call, etc.)
7//!
8//! # FFI Functions
9//!
10//! The following functions are exported with C linkage:
11//!
12//! - `plugin_init` - Initialize a plugin instance
13//! - `plugin_call` - Make a synchronous request to the plugin
14//! - `plugin_free_buffer` - Free a buffer returned by plugin_call
15//! - `plugin_shutdown` - Shutdown a plugin instance
16//! - `plugin_set_log_level` - Set the log level for a plugin
17
18mod binary_types;
19mod buffer;
20mod exports;
21mod handle;
22mod panic_guard;
23
24pub use binary_types::{RbBytes, RbBytesOwned, RbResponse, RbString, RbStringOwned};
25pub use buffer::FfiBuffer;
26pub use handle::{PluginHandle, PluginHandleManager};
27
28// Re-export FFI functions for use by plugins
29pub use exports::{
30    BinaryMessageHandler, plugin_call, plugin_call_raw, plugin_free_buffer,
31    plugin_get_rejected_count, plugin_get_state, plugin_init, plugin_set_log_level,
32    plugin_shutdown, rb_response_free, register_binary_handler,
33};
34
35// Re-export types needed for plugin implementation
36pub use rustbridge_core::{LogLevel, Plugin, PluginConfig, PluginContext, PluginError};
37pub use rustbridge_logging::LogCallback;
38pub use rustbridge_runtime::{AsyncBridge, AsyncRuntime, RuntimeConfig};
39pub use rustbridge_transport::{RequestEnvelope, ResponseEnvelope};
40
41/// Prelude module for convenient imports
42pub mod prelude {
43    pub use crate::{
44        FfiBuffer, PluginHandle, PluginHandleManager, RbBytes, RbBytesOwned, RbResponse, RbString,
45        RbStringOwned,
46    };
47    pub use rustbridge_core::prelude::*;
48    pub use rustbridge_logging::prelude::*;
49    pub use rustbridge_runtime::prelude::*;
50    pub use rustbridge_transport::prelude::*;
51}
52
53/// Macro to generate the FFI entry point for a plugin
54///
55/// This macro creates the necessary `extern "C"` functions that the host
56/// will call to interact with the plugin.
57///
58/// # Example
59///
60/// ```ignore
61/// use rustbridge_ffi::prelude::*;
62///
63/// struct MyPlugin;
64///
65/// // ... implement Plugin trait ...
66///
67/// rustbridge_ffi::plugin_entry!(MyPlugin::new);
68/// ```
69#[macro_export]
70macro_rules! plugin_entry {
71    ($factory:expr_2021) => {
72        #[unsafe(no_mangle)]
73        pub unsafe extern "C" fn plugin_create() -> *mut std::ffi::c_void {
74            let plugin = Box::new($factory());
75            Box::into_raw(plugin) as *mut std::ffi::c_void
76        }
77    };
78}