For Rust-to-Rust ffi, with a focus on creating libraries loaded at program startup, and with load-time type-checking.
This library allows defining Rust libraries that can be loaded at runtime, even if they were built with a different Rust version than the crate that depends on it.
These are some usecases for this library:
Converting a Rust dependency tree from compiling statically into a single binary, into one binary (and potentially) many dynamic libraries, allowing separate re-compilation on changes.
Creating a plugin system (without support for unloading).
Currently this library has these features:
sabi_traitattribute macro, for creating ffi-safe trait objects.
Ffi-safe equivalent of some trait objects with
Provides ffi-safe alternatives/wrappers for many standard library types, in the
Provides ffi-safe wrappers for some types defined in external crates, in the
StableAbitrait for asserting that types are ffi-safe.
The prefix types feature for building extensible modules and vtables, without breaking ABI compatibility.
Checking at load-time that the types in the dynamic library have the expected layout, allowing for semver compatible changes while checking the layout of types.
StableAbiderive macro to both assert that the type is ffi compatible, and to get the layout of the type at load-time to check that it is still compatible.
For examples of using
abi_stable you can look at the readme example,
or for the crates in the examples directory in the repository for this crate.
This crate also has examples in the docs for most features.
To run the example crates you'll generally have to build the
then run the
*_user crate (all
*_user crates should have a help message and a readme.md).
interface crate:the crate that declares the public functions, types, and traits that
are necessary to load the library at runtime.
ìmplementation crate:A crate that implements all the functions in the interface crate.
user crate:A crate that depends on an
interface crate and
loads 1 or more
ìmplementation crates for it.
module:refers to a struct of function pointers and other static values.
The root module implement the
These are declared in the
interface crate,exported in the
and loaded in the
For how to evolve dynamically loaded libraries you can look at the library_evolution module.
These are the kinds of types passed through FFI:
This is the default kind when deriving StableAbi. The layout of these types must not change in a minor versions.
Trait objects :
Trait object-like types generated using the
sabi_traitattribute macro, which erase the type of the value they wrap,implements the methods of the trait, and can only be unwrapped back to the original type in the dynamic library/binary that created it.
Types wrapped in
DynTrait, whose layout can change in any version of the library, and can only be unwrapped back to the original type in the dynamic library/binary that created it.
Prefix types :
Types only accessible through some custom pointer types, most commonly vtables and modules, which can be extended in minor versions while staying ABI compatible, by adding fields at the end.
Unsafe code guidelines :
Describes how to write unsafe code ,relating to this library.
Some problems and their solutions.
For generating ffi-safe trait objects.
For asserting abi-stability of a type, and obtaining the layout of the type at runtime.
Nonexhaustive enums :
Details for how to declare nonexhaustive enums.
Prefix types (using the StableAbi derive macro):
The method by which vtables and modules are implemented, allowing extending them in minor versions of a library.
types and traits related to abi stability.
Utilities for const contexts.
Documentation for macros,and guides.
Types and traits related to type erasure.
Ffi wrapper for types defined outside the standard library.
Types used in documentation examples.
Traits and types related to loading an abi_stable dynamic library, as well as functions/modules within.
Zero-sized types .
Contains types and traits for nonexhaustive enums.
Traits for pointers.
Types,traits,and functions used by prefix-types.
Miscelaneous items re-exported from core_extensions.
Types and submodules for doing runtime reflection.
Contains items related to the
ffi-safe types that aren't wrappers for other types.
Contains many ffi-safe equivalents of standard library types. The vast majority of them can be converted to and from std equivalents.
Where miscellaneous traits reside.
Types for modeling the layout of a datatype
Types used to represent values at compile-time,eg:True/False.
Use this macro to get the type of a
Use this to make sure that you handle panics inside
A macro to construct
A macro to construct
Use this macro to construct a
Allows declaring a
Use this when constructing a
Allows converting between
DynTrait implements ffi-safe trait objects,for a selection of traits.
The header used to identify the version number of abi_stable that a dynamic libraries uses.
Defines the usable/required traits when creating a
Represents a type whose layout is stable.
This attribute is used for functions which export a module in an
This macro is documented in abi_stable::docs::sabi_extern_fn
This macro is documented in
This macro is documented in
This macro is documented in abi_stable::docs::stable_abi_derive