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//! - `plugin_call_async` - Make an async request (future)
18//! - `plugin_cancel_async` - Cancel an async request (future)
19
20mod binary_types;
21mod buffer;
22mod exports;
23mod handle;
24mod panic_guard;
25
26pub use binary_types::{RbBytes, RbBytesOwned, RbResponse, RbString, RbStringOwned};
27pub use buffer::FfiBuffer;
28pub use handle::{PluginHandle, PluginHandleManager};
29
30// Re-export FFI functions for use by plugins
31pub use exports::{
32    BinaryMessageHandler, plugin_call, plugin_call_async, plugin_call_raw, plugin_cancel_async,
33    plugin_free_buffer, plugin_get_rejected_count, plugin_get_state, plugin_init,
34    plugin_set_log_level, plugin_shutdown, rb_response_free, register_binary_handler,
35};
36
37// Re-export types needed for plugin implementation
38pub use rustbridge_core::{LogLevel, Plugin, PluginConfig, PluginContext, PluginError};
39pub use rustbridge_logging::LogCallback;
40pub use rustbridge_runtime::{AsyncBridge, AsyncRuntime, RuntimeConfig};
41pub use rustbridge_transport::{RequestEnvelope, ResponseEnvelope};
42
43/// Prelude module for convenient imports
44pub mod prelude {
45    pub use crate::{
46        FfiBuffer, PluginHandle, PluginHandleManager, RbBytes, RbBytesOwned, RbResponse, RbString,
47        RbStringOwned,
48    };
49    pub use rustbridge_core::prelude::*;
50    pub use rustbridge_logging::prelude::*;
51    pub use rustbridge_runtime::prelude::*;
52    pub use rustbridge_transport::prelude::*;
53}
54
55/// Macro to generate the FFI entry point for a plugin
56///
57/// This macro creates the necessary `extern "C"` functions that the host
58/// will call to interact with the plugin.
59///
60/// # Example
61///
62/// ```ignore
63/// use rustbridge_ffi::prelude::*;
64///
65/// struct MyPlugin;
66///
67/// // ... implement Plugin trait ...
68///
69/// rustbridge_ffi::plugin_entry!(MyPlugin::new);
70/// ```
71#[macro_export]
72macro_rules! plugin_entry {
73    ($factory:expr_2021) => {
74        #[unsafe(no_mangle)]
75        pub unsafe extern "C" fn plugin_create() -> *mut std::ffi::c_void {
76            let plugin = Box::new($factory());
77            Box::into_raw(plugin) as *mut std::ffi::c_void
78        }
79    };
80}