#dlopen #dylib #define

savefile-abi

为Rust库提供易于使用、简单、稳定的ABI。允许创建用Rust编写的可动态加载的插件。

19个版本

新版本 0.17.7 2024年8月3日
0.17.6 2024年7月2日
0.17.4 2024年5月12日
0.17.0-beta.152024年4月30日

#117 in FFI

Download history 315/week @ 2024-04-15 142/week @ 2024-04-22 827/week @ 2024-04-29 312/week @ 2024-05-06 115/week @ 2024-05-13 6/week @ 2024-05-20 2/week @ 2024-05-27 5/week @ 2024-06-10 289/week @ 2024-07-01 300/week @ 2024-07-29

每月303次下载

MIT/Apache

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尚未成熟。

请参阅完整文档:https://docs.rs/savefile-abi/latest/

依赖项

~2.9–8.5MB
~71K SLoC