Expand description
§rustbridge
A framework for developing Rust shared libraries callable from other languages.
rustbridge uses C ABI under the hood but abstracts the complexity, providing:
- OSGI-like lifecycle management
- Mandatory async (Tokio) runtime
- Logging callbacks to host language
- JSON-based data transport with optional binary transport
§Quick Start
Add to your Cargo.toml:
[lib]
crate-type = ["cdylib"]
[dependencies]
rustbridge = "0.6"§Creating a Plugin
ⓘ
use rustbridge::prelude::*;
#[derive(Debug, Serialize, Deserialize, Message)]
#[message(tag = "echo")]
pub struct EchoRequest {
pub message: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EchoResponse {
pub message: String,
}
#[derive(Default)]
pub struct MyPlugin;
#[async_trait]
impl Plugin for MyPlugin {
async fn on_start(&self, _ctx: &PluginContext) -> PluginResult<()> {
tracing::info!("Plugin started");
Ok(())
}
async fn handle_request(
&self,
_ctx: &PluginContext,
type_tag: &str,
payload: &[u8],
) -> PluginResult<Vec<u8>> {
match type_tag {
"echo" => {
let req: EchoRequest = serde_json::from_slice(payload)?;
Ok(serde_json::to_vec(&EchoResponse { message: req.message })?)
}
_ => Err(PluginError::UnknownMessageType(type_tag.to_string())),
}
}
async fn on_stop(&self, _ctx: &PluginContext) -> PluginResult<()> {
tracing::info!("Plugin stopped");
Ok(())
}
}
// Generate FFI entry point
rustbridge_entry!(MyPlugin::default);
// Re-export FFI functions for the shared library
pub use rustbridge::ffi_exports::*;§Crate Structure
This is a facade crate that re-exports from:
rustbridge_core- Core traits, types, and lifecyclerustbridge_macros- Procedural macros (Message,rustbridge_entry!)rustbridge_ffi- FFI exports and buffer management
Re-exports§
pub use serde;pub use serde_json;pub use tokio;pub use tracing;
Modules§
- ffi_
exports - FFI function exports that plugins must re-export.
- prelude
- Prelude module for convenient imports.
Macros§
- impl_
plugin - Macro to implement the Plugin trait with handler dispatch
- rustbridge_
entry - Generate the FFI entry point for a plugin
Structs§
- FfiBuffer
- Buffer for passing data across FFI boundary
- Plugin
Config - Plugin configuration passed during initialization
- Plugin
Context - Context provided to plugin operations
- Plugin
Handle - Handle to an initialized plugin instance
- Plugin
Handle Manager - Manages plugin handles
- Plugin
Metadata - Plugin metadata from bundle manifest
- Request
Context - Context for an incoming request
- Response
Builder - Builder for constructing responses
Enums§
- Lifecycle
State - Plugin lifecycle states following OSGI-inspired model
- LogLevel
- Log levels for FFI callbacks
- Plugin
Error - Error type for plugin operations
Traits§
- Plugin
- Main trait for implementing rustbridge plugins
- Plugin
Factory - Factory trait for creating plugins with optional configuration.
Functions§
- register_
binary_ handler - Register a binary message handler
Type Aliases§
- Plugin
Result - Result type alias for plugin operations
Attribute Macros§
- async_
trait - rustbridge_
handler - Attribute for marking a method as a message handler
- rustbridge_
plugin - Attribute for marking a struct as a rustbridge plugin
Derive Macros§
- Message
- Derive macro for message types