ffi_rpc
Use FFI with RPC! The ABI is stable, any serializable type can be safely transferred through the FFI boundary.
Why this crate
It has been quite a long time that the Rust does not have a stable ABI. Developing a plugin system is a challenging work since we have meet several problems such as Segmentation fault, Bus error and unexpected behaviors across the FFI boundary with libloading. The bugs exist only at runtime, depending on a lot of variables (OS type, rustc version, etc.). It is a nightmare to debug these errors.
Luckily, we have abi_stable crate with can provide a working stable ABI for us. However, it would be tricky and complex to introduce customized types to the interface. Thus, we have this crate to transfer any serializable type across the FFI boundary.
Limitations
- Generic is not supported.
- Panic on incompatible API/Library, please manage your API version by yourself.
Quick start
Assume you have three projects:
- server: typically the binary which will load dynamic libraries and use the FFI functions.
- client(plugin): the
.dylib/.so/.dlllibrary that defines the interface. - client_interface: the interface that will be shared between server and client.
client_interface
- Add
ffi_rpcto[dependencies]inCargo.toml. - In
lib.rs:use ;
How to split one interface into multiple traits: example.
client
- Add
abi_stable = "0.11"andffi_rpcto[dependencies]inCargo.toml. - In
lib.rs:use ; // must use full path ;
How to implement multiple interfaces: example.
How to invoke other clients: example.
server
- Init the registry:
let mut r = default; - Init all clients:
let lib = new.unwrap; - Invoke methods:
let ret = lib.add.await;
How to mock a client: example.
Black magic
Customize _ffi_call to route to different implementations manually.