19个版本
新版本 0.17.7 | 2024年8月3日 |
---|---|
0.17.6 | 2024年7月2日 |
0.17.4 | 2024年5月12日 |
0.17.0-beta.15 | 2024年4月30日 |
#117 in FFI
每月303次下载
415KB
7.5K SLoC
欢迎使用Savefile-abi!
完整文档:https://docs.rs/savefile-abi/latest/
Savefile-abi是一个主要用于帮助使用Rust构建二进制插件的crate。
savefile-abi = "0.17"
savefile = "0.17"
savefile-derive = "0.17"
示例
假设我们有一个crate定义了这个trait用于添加u32
interface_crate
use savefile_derive::savefile_abi_exportable;
#[savefile_abi_exportable(version=0)]
pub trait AdderInterface {
fn add(&self, x: u32, y: u32) -> u32;
}
现在,我们想在不同的crate中实现加法,将其编译为共享库(.dll或.so),并在第一个crate(或另一个crate)中使用它
implementation_crate
use interface_crate::{AdderInterface};
use savefile_derive::savefile_abi_export;
#[derive(Default)]
struct MyAdder { }
impl AdderInterface for MyAdder {
fn add(&self, x: u32, y: u32) -> u32 {
x + y
}
}
// Export this implementation as the default-implementation for
// the interface 'AdderInterface', for the current library.
savefile_abi_export!(MyAdder, AdderInterface);
我们在实现crate的Cargo.toml中添加以下内容
[lib]
crate-type = ["cdylib"]
现在,在我们的应用程序中,我们添加了对interface_crate的依赖,但没有添加对ImplementationCrate的依赖。
然后我们在运行时动态加载实现
app
use savefile_abi::{AbiConnection};
use interface_crate::{AdderInterface};
fn main() {
// Load the implementation of `dyn AdderInterface` that was published
// using the `savefile_abi_export!` above.
let connection = AbiConnection::<dyn AdderInterface>
::load_shared_library("./ImplementationCrate.so").unwrap();
// The type `AbiConnection::<dyn AdderInterface>` implements
// the `AdderInterface`-trait, so we can use it to call its methods.
assert_eq!(connection.add(1, 2), 3);
}
限制
存在多个限制
- 目前不支持将元组作为直接函数参数!
- 可能存在安全问题,Savefile-Abi尚未成熟。
依赖项
~2.9–8.5MB
~71K SLoC