8个版本 (5个破坏性更新)

0.6.2 2019年7月28日
0.6.0 2019年7月24日
0.5.0 2019年6月14日
0.4.0 2019年5月23日
0.1.1 2019年4月14日

#2760 in Rust模式

MIT/Apache

340KB
9K SLoC

Rust Join the chat at https://gitter.im/abi_stable_crates/community api-docs

适用于Rust-to-Rust ffi,侧重于在程序启动时加载的库,以及加载时类型检查。

此库允许定义可以在运行时加载的Rust库。由于默认(Rust)ABI和表示不稳定,因此这不可能。

此库的一些用例

  • 将Rust依赖树从编译为单个二进制文件转换为单个二进制文件(以及可能许多动态库),允许在更改时单独重新编译。

  • 创建插件系统(不支持卸载)。

特性

目前此库具有以下特性

  • 使用 sabi_trait 属性宏创建ffi安全的trait对象。

  • 使用 DynTrait 为一些trait对象提供ffi安全的等价物。

  • std_types 模块中为许多标准库类型提供ffi安全的替代品/包装器。

  • external_types 模块中为外部crate中定义的一些类型提供ffi安全的包装器。

  • 提供 StableAbi trait,用于断言类型是ffi安全的。

  • 提供 前缀类型 功能,用于构建可扩展的模块和vtables,而不会破坏ABI兼容性。

  • 支持包装在 NonExhaustive 中的ffi安全的非穷举枚举

  • 在加载时检查动态库中的类型布局是否符合预期,允许在检查类型布局的同时进行与semver兼容的更改。

  • 提供StableAbi derive宏,既断言类型是ffi兼容的,又可以在加载时获取类型的布局以检查其是否仍然兼容。

变更日志

变更日志位于“Changelog.md”文件中。

示例crate

对于使用abi_stable的示例crate,您可以查看此crate仓库中示例目录下的crate。

要运行示例crate,通常需要构建*_impl crate,然后运行*_user crate(所有*_user crate都应有帮助信息)。

这些是示例crate

  • 0 - 模块和接口类型:通过一个具有动态链接后端的命令行应用程序演示abi_stable的“模块”(函数指针的struct)和接口类型。

  • 1 - 特征对象:通过创建最小插件系统演示ffi安全的特征对象(使用sabi_trait属性宏生成)。

  • 2 - 非穷举枚举:演示非穷举枚举作为参数和返回值,用于管理商店目录的应用程序。

示例

这是一个完整的示例,位于examples/readme_example,演示

  • user crates(定义在下面的架构部分)。

  • 通过sabi_trait属性宏生成的ffi安全特征对象。

  • DynTrait:一个ffi安全的多特征对象,可以选择一组特征,也可以将其下转换为具体类型。

  • interface crates(定义在下面的架构部分)。

  • 实现crate(定义在下面的架构部分)。

用户crate

此用户crate(也称为“应用程序crate”)依赖于接口crate,

[dependencies.readme_interface]
path = "../readme_interface" 

其Rust代码是

use abi_stable::std_types::RVec;

use readme_interface::{
    load_root_module_in_directory, AppenderBox, Appender_TO, BoxedInterface, ExampleLib_Ref,
};

fn main() {
    // The type annotation is for the reader
    let library: ExampleLib_Ref = load_root_module_in_directory("../../../target/debug".as_ref())
        .unwrap_or_else(|e| panic!("{}", e));

    {
        /////////////////////////////////////////////////////////////////////////////////
        //
        //       This block demonstrates `#[sabi_trait]` generated trait objects
        //
        ////////////////////////////////////////////////////////////////////////////////

        // The type annotation is for the reader
        let mut appender: AppenderBox<u32> = library.new_appender()();
        appender.push(100);
        appender.push(200);

        // The primary way to use the methods in the trait is through the inherent methods on
        // the ffi-safe trait object.
        Appender_TO::push(&mut appender, 300);
        appender.append(vec![500, 600].into());
        assert_eq!(
            appender.into_rvec(),
            RVec::from(vec![100, 200, 300, 500, 600])
        );
    }
    {
        ///////////////////////////////////////////////////////////////////////////////////
        //
        //  This block demonstrates the `DynTrait<>` trait object.
        //
        //  `DynTrait` is used here as a safe opaque type which can only be unwrapped back to
        //  the original type in the dynamic library that constructed the `DynTrait` itself.
        //
        ////////////////////////////////////////////////////////////////////////////////////

        // The type annotation is for the reader
        let mut unwrapped: BoxedInterface = library.new_boxed_interface()();

        library.append_string()(&mut unwrapped, "Hello".into());
        library.append_string()(&mut unwrapped, ", world!".into());

        assert_eq!(&*unwrapped.to_string(), "Hello, world!");
    }

    println!("success");
}

注意:必须在运行此程序之前编译实现crate,否则您将获得运行时错误,因为库无法加载。

接口crate

use std::path::Path;

use abi_stable::{
    library::{LibraryError, RootModule},
    package_version_strings, sabi_trait,
    sabi_types::VersionStrings,
    std_types::{RBox, RString, RVec},
    DynTrait, StableAbi,
};

/// This struct is the root module,
/// which must be converted to `ExampleLib_Ref` to be passed through ffi.
///
/// The `#[sabi(kind(Prefix(prefix_ref = ExampleLib_Ref)))]`
/// attribute tells `StableAbi` to create an ffi-safe static reference type
/// for `ExampleLib` called `ExampleLib_Ref`.
///
/// The `#[sabi(missing_field(panic))]` attribute specifies that trying to
/// access a field that doesn't exist must panic with a message saying that
/// the field is inaccessible.
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = ExampleLib_Ref)))]
#[sabi(missing_field(panic))]
pub struct ExampleLib {
    pub new_appender: extern "C" fn() -> AppenderBox<u32>,

    pub new_boxed_interface: extern "C" fn() -> BoxedInterface<'static>,

    /// The `#[sabi(last_prefix_field)]` attribute here means that this is the last
    /// field in this struct that was defined in the first compatible version of the library
    /// (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
    /// requiring new fields to always be added below preexisting ones.
    ///
    /// The `#[sabi(last_prefix_field)]` attribute would stay on this field until the
    /// library bumps its "major" version,
    /// at which point it would be moved to the last field at the time.
    ///
    #[sabi(last_prefix_field)]
    pub append_string: extern "C" fn(&mut BoxedInterface<'_>, RString),
}

/// The RootModule trait defines how to load the root module of a library.
impl RootModule for ExampleLib_Ref {
    abi_stable::declare_root_module_statics! {ExampleLib_Ref}

    const BASE_NAME: &'static str = "example_library";
    const NAME: &'static str = "example_library";
    const VERSION_STRINGS: VersionStrings = package_version_strings!();
}

/// This loads the root from the library in the `directory` folder.
pub fn load_root_module_in_directory(directory: &Path) -> Result<ExampleLib_Ref, LibraryError> {
    ExampleLib_Ref::load_from_directory(directory)
}

//////////////////////////////////////////////////////////

/// `#[sabi_trait]` is how one creates an ffi-safe trait object from a trait definition.
///
/// In this case, the trait object is `Appender_TO<'lt, Pointer<()>, Element>`,where:
///
/// - `'lt`:
///     Is the lifetime bound of the type that constructed the trait object
///     (`'static` is the lifetime bound of objects that don't borrow anything).
///
/// - `Pointer<()>`:
///     Is any pointer that implements some abi_stable specific traits,
///     this pointer owns the value that implements `Appender`.
///
/// - `Element`:
///     This is the element type of the collection that we operate on.
///     This is a type parameter because it's a trait object,
///     which turn associated types into type parameters.
///
#[sabi_trait]
pub trait Appender {
    /// The element type of the collection.
    type Element;

    /// Appends one element at the end of the collection.    
    fn push(&mut self, value: Self::Element);

    /// Appends many elements at the end of the collection.    
    fn append(&mut self, vec: RVec<Self::Element>);

    /// Converts this collection into an `RVec`.
    ///
    /// As opposed to regular trait objects,
    /// it is possible to call by-value methods on trait objects generated by `#[sabi_trait]`.
    ///
    /// The `#[sabi(last_prefix_field)]` attribute here means that this is the last method
    /// that was defined in the first compatible version of the library
    /// (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
    /// requiring new methods to always be added below preexisting ones.
    ///
    /// The `#[sabi(last_prefix_field)]` attribute would stay on this method until the library
    /// bumps its "major" version,
    /// at which point it would be moved to the last method at the time.
    ///
    #[sabi(last_prefix_field)]
    fn into_rvec(self) -> RVec<Self::Element>;
}

/// A type alias for the Appender trait object.
///
/// `'static` here means that the trait object cannot contain any borrows.
pub type AppenderBox<T> = Appender_TO<'static, RBox<()>, T>;

// Impls of local traits for dependencies have to be implemented in
// the interface crate, because of the orphan rules.
//
// To avoid compiling more code than necessary,
// this impl is not compiled by default.
// it's enabled by the implementation crate but not the user crate.
#[cfg(feature = "impls")]
impl<T> Appender for RVec<T> {
    type Element = T;
    fn push(&mut self, value: Self::Element) {
        self.push(value);
    }
    fn append(&mut self, vec: RVec<Self::Element>) {
        self.extend(vec);
    }
    fn into_rvec(self) -> RVec<Self::Element> {
        self
    }
}

//////////////////////////////////////////////////////////

/// This type implements `ÌnterfaceType`
/// (because of the `#[sabi(impl_InterfaceType())]` helper attribute of `#[derive(StableAbi)]` ),
/// describing the traits required when constructing `DynTrait<_, TheInterface>`,
/// and are then implemented by it.
#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Sync, Send, Debug, Display))]
pub struct TheInterface;

/// An alias for the trait object used in this example
pub type BoxedInterface<'borr> = DynTrait<'borr, RBox<()>, TheInterface>;

实现crate

这是实现crate,它作为cdylib(动态库/共享对象)进行编译,并在运行时由用户crate加载。

其Cargo.toml文件中的重要部分是

[lib]
name = "readme_library"
crate-type = ["cdylib",'rlib']

[dependencies.readme_interface]
path = "../readme_interface" 
features = ["impls"]

其Rust代码是

use std::fmt::{self, Display};

use readme_interface::{AppenderBox, Appender_TO, BoxedInterface, ExampleLib, ExampleLib_Ref};

use abi_stable::{
    export_root_module,
    prefix_type::PrefixTypeTrait,
    sabi_extern_fn,
    sabi_trait::prelude::TD_Opaque,
    std_types::{RString, RVec},
    DynTrait,
};

/// The function which exports the root module of the library.
///
/// The root module is exported inside a static of `LibHeader` type,
/// which has this extra metadata:
///
/// - The abi_stable version number used by the dynamic library.
///
/// - A constant describing the layout of the exported root module,and every type it references.
///
/// - A lazily initialized reference to the root module.
///
/// - The constructor function of the root module.
///
#[export_root_module]
pub fn get_library() -> ExampleLib_Ref {
    ExampleLib {
        new_appender,
        new_boxed_interface,
        append_string,
    }
    .leak_into_prefix()
}

/// `DynTrait<_, TheInterface>` is constructed from this type in this example
#[derive(Debug, Clone)]
pub struct StringBuilder {
    pub text: String,
    pub appended: Vec<RString>,
}

impl Display for StringBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.text, f)
    }
}

impl StringBuilder {
    /// Appends the string at the end.
    pub fn append_string(&mut self, string: RString) {
        self.text.push_str(&string);
        self.appended.push(string);
    }
}

#[sabi_extern_fn]
pub fn new_appender() -> AppenderBox<u32> {
    // What `TD_Opaque` does here is specify that the trait object cannot be downcasted,
    // disallowing the `Appender_TO` from being unwrapped back into an `RVec<u32>`
    // when the `trait_object.obj.*_downcast_*()` methods are used.
    //
    // To be able to unwrap a `#[sabi_trait]` trait object back into the type it
    // was constructed with, you must:
    //
    // - Have a type that implements `std::anu::Any`
    // (it requires that the type doesn't borrow anything).
    //
    // - Pass `TD_CanDowncast` instead of `TD_Opaque` to
    // `Appender_TO::{from_const, from_value,from_ptr}`.
    //
    // - Unerase the trait object back into the original type with
    //     `trait_object.obj.downcast_into::<RVec<u32>>().unwrap()`
    //     (or the other downcasting methods).
    //
    // Downcasting a trait object will fail in any of these conditions:
    //
    // - It wasn't constructed in the same dynamic library.
    //
    // - It's not the same type.
    //
    // - It was constructed with `TD_Opaque`.
    //
    Appender_TO::from_value(RVec::new(), TD_Opaque)
}

/// Constructs a BoxedInterface.
#[sabi_extern_fn]
fn new_boxed_interface() -> BoxedInterface<'static> {
    DynTrait::from_value(StringBuilder {
        text: "".into(),
        appended: vec![],
    })
}

/// Appends a string to the erased `StringBuilder`.
#[sabi_extern_fn]
fn append_string(wrapped: &mut BoxedInterface<'_>, string: RString) {
    wrapped
        .downcast_as_mut::<StringBuilder>() // Returns `Result<&mut StringBuilder, _>`
        .unwrap() // Returns `&mut StringBuilder`
        .append_string(string);
}

安全性

此库确保通过这些机制加载的库是安全的。

  • 检查库的abi_stable ABI,每个0.y.0版本和x.0.0版本的abi_stable定义了自己的ABI,与先前版本不兼容。

  • 在调用任何函数之前,在加载动态库时递归检查类型。

请注意,此库假定动态库来自良性的源,这些检查纯粹是为了检测编程错误。

计划的功能

目前没有。

非功能特性(极不可能添加)

支持库卸载,因为这将要求构建整个库,假设任何东西都可能在任何时候卸载。

架构

这是用户可以组织其库以允许动态链接的一种方式。

有关如何在abi_stable中使用安全API加载的动态库进行演化的方法,请查看此处:链接

接口crate

一个声明了

  • 根模块(一个函数指针/其他模块的结构体),该模块实现了从动态库导出的RootModule特质。

  • 根模块的所有子模块。

  • 所有传递给函数并从函数返回的公共类型。

  • 可选:使用sabi_trait属性声明ffi安全的特质,用作公共接口中的特质对象。

  • 可选:声明接口类型,这些类型实现了InterfaceType,用于指定在DynTrait ffi安全的特质对象中可用的特质。

实现crate

将编译为动态库的crate

  • 实现了在interface crate中声明的所有函数。

  • 声明一个导出根模块的函数,使用export_root_module属性来导出模块。

  • 可选:实现带有sabi_trait属性注解的特质,构建其特质对象并在公共API中公开。

用户crate

一个声明了interface crate作为依赖项的crate,并从某个路径加载预编译的implementation crate动态库。

最小Rust版本

此crate支持Rust回退到1.61.0

您可以使用rust_*_* cargo功能手动启用对Rust 1.61.0之后版本的支持。

crate功能

这些是默认的cargo功能,启用可选crate

  • "channels": 依赖于crossbeam-channel,用于ffi,在abi_stable::external_types::crossbeam_channel中包装通道。

  • "serde_json": 依赖于serde_json,在abi_stable::external_types::serde_json中提供ffi安全的等效于&serde_json::value::RawValueBox<serde_json::value::RawValue>的功能。

要禁用默认功能,请使用

[dependencies.abi_stable]
version = "<current_version>"
default-features = false
features = [  ]

features数组中启用您需要的功能。

手动启用

这些是手动启用对新语言功能支持的crate功能

  • "rust_1_64": 将许多将类型转换为切片的函数转换为const fns。

  • "rust_latest_stable": 启用所有稳定版本的所有"rust_1_*"功能。

术语表

interface crate:声明了在运行时加载库所需的公共函数、类型和特质的crate。

ìmplementation crate:实现接口crate中所有函数的crate。

user crate:依赖于一个 interface crate 并为其加载 1 个或多个 implementation crate 的 crate。

module:指代一个包含函数指针和其他静态值的 struct。库的根模块实现了 RootModule trait。这些在 interface crate 中声明,在 implementation crate 中导出,并在 user crate 中加载。

工具

以下是一些工具,它们都位于 "tools" 目录(文件夹)中。

sabi_extract

一个从 abi_stable 动态库中提取各种信息的程序。

许可证

abi_stable 根据 Apache-2.0 许可证 授权。

    Apache License, Version 2.0, (LICENSE-APACHE or https://apache.ac.cn/licenses/LICENSE-2.0)
    MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

您可选择。

贡献

除非您明确说明,否则您提交给 abi_stable 的任何贡献,根据 Apache-2.0 许可证定义,将按上述方式双授权,不附加任何额外条款或条件。

依赖项

~2.5MB
~54K SLoC