Expand description
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).
§Features
Currently this library has these features:
-
Features the
sabi_trait
attribute macro, for creating ffi-safe trait objects. -
Ffi-safe equivalent of some trait objects with
DynTrait
. -
Provides ffi-safe alternatives/wrappers for many standard library types, in the
std_types
module. -
Provides ffi-safe wrappers for some types defined in external crates, in the
external_types
module. -
Provides the
StableAbi
trait for asserting that types are ffi-safe. -
The prefix types feature for building extensible modules and vtables, without breaking ABI compatibility.
-
Supports ffi-safe nonexhaustive enums, wrapped in
NonExhaustive
. -
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.
-
Provides the
StableAbi
derive 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.
§Examples
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 *_impl
crate,
then run the *_user
crate (all *_user
crates should have a help message and a readme.md).
§Minimum Rust version
This crate support Rust back to 1.61.0
You can manually enable support for Rust past 1.61.0 with the rust_*_*
cargo features.
§Crate Features
These are default cargo features that enable optional crates :
-
“channels”: Depends on
crossbeam-channel
, wrapping channels from it for ffi inabi_stable::external_types::crossbeam_channel
. -
“serde_json”: Depends on
serde_json
, providing ffi-safe equivalents of&serde_json::value::RawValue
andBox<serde_json::value::RawValue>
, inabi_stable::external_types::serde_json
.
To disable the default features use:
[dependencies.abi_stable]
version = "<current_version>"
default-features = false
features = [ ]
enabling the features you need in the features
array.
§Manually enabled
These are crate features to manually enable support for newer language features:
-
“rust_1_64”: Turns many functions for converting types to slices into const fns.
-
“rust_latest_stable”: Enables the “rust_1_*” features for all the stable releases.
§Glossary
interface crate
: the crate that declares the public functions, types, and traits that
are necessary to load a library at runtime.
ìmplementation crate
: A crate that implements all the functions in a interface crate.
user crate
: A crate that depends on an interface crate
and
loads 1 or more ìmplementation crate
s for it.
module
: refers to a struct of function pointers and other static values.
The root module of a library implements the RootModule
trait.
These are declared in the interface crate
,exported in the implementation crate
,
and loaded in the user crate
.
§Rust-to-Rust FFI types.
Types must implement StableAbi
to be safely passed through the FFI boundary,
which can be done using the StableAbi
derive macro.
For how to evolve dynamically loaded libraries you can look at the library_evolution module.
These are the kinds of types passed through FFI:
-
Value kind:
This is the default kind when deriving StableAbi. The layout of these types must not change in a minor versions. -
Nonexhaustive enums :
Enums wrapped insideNonExhaustive
, which can add variants in minor versions of the library. -
Trait objects :
Trait object-like types generated using thesabi_trait
attribute 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. -
Opaque kind:
Types wrapped inDynTrait
, 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.
§Extra documentation
-
Unsafe code guidelines :
Describes how to write unsafe code ,relating to this library. -
Troubleshooting :
Some problems and their solutions.
§Macros (derive and attribute)
-
sabi_trait
attribute macro:
For generating ffi-safe trait objects. -
StableAbi
derive :
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 way that vtables and modules are implemented, allowing extending them in minor versions of a library.
Re-exports§
Modules§
- abi_
stability - types and traits related to abi stability.
- const_
utils - Utilities for const contexts.
- docs
- Additional docs for macros, and guides.
- erased_
types - Types and traits related to type erasure.
- external_
types - Ffi wrapper for types defined outside the standard library.
- for_
examples - Types used in documentation examples.
- inline_
storage - Contains the
InlineStorage
trait,and related items. - library
- Traits and types related to loading an abi_stable dynamic library, as well as functions/modules within.
- marker_
type - Zero-sized types .
- nonexhaustive_
enum - Contains types and traits for nonexhaustive enums.
- pointer_
trait - Traits for pointers.
- prefix_
type - Types,traits,and functions used by prefix-types.
- reexports
- Miscelaneous items re-exported from core_extensions.
- reflection
- Types and submodules for doing runtime reflection.
- sabi_
trait - Contains items related to the
#[sabi_trait]
attribute. - sabi_
types - ffi-safe types that aren’t wrappers for other types.
- std_
types - Contains many ffi-safe equivalents of standard library types. The vast majority of them can be converted to and from std equivalents.
- traits
- Where miscellaneous traits reside.
- type_
layout - Types for modeling the layout of a datatype
- type_
level - Types used to represent values at compile-time, eg: True/False.
- utils
- Utility functions.
Macros§
- RTuple
- Use this macro to get the type of a
Tuple*
with the types passed to the macro. - declare_
root_ module_ statics - Implements the
RootModule::root_module_statics
associated function. - extern_
fn_ panic_ handling - Use this to make sure that you handle panics inside
extern fn
correctly. - make_
item_ info - Constructs an
ItemInfo
, with information about the place where it’s called. - nulstr
- Constructs a
NulStr
from a string literal. - nulstr_
trunc - Constructs a
NulStr
from a string literal, truncating the string on internal nul bytes. - package_
version_ strings - Instantiates a
VersionStrings
with the major.minor.patch version of the library where it is invoked. - rslice
- A macro to construct
RSlice
s. - rstr
- Constructs
RStr
constants from&'static str
constants. - rtry
- Equivalent to
?
forRResult
. - rtry_
opt - Equivalent to
?
forROption
. - rtuple
- Use this macro to construct a
abi_stable::std_types::Tuple*
with the values passed to the macro. - rvec
- Constructs an
RVec
using the same syntax that thestd::vec
macro uses. - staticref
- Allows declaring a
StaticRef
inherent associatedconst
ant from possibly non-'static
references. - tag
- Constructs a
Tag
, a dynamically typed value for users to check extra properties about their types when doing runtime type checking. - tl_
genparams - Can be used to construct
CompGenericParams
, when manually implementingStableAbi
.
Structs§
- DynTrait
- DynTrait implements ffi-safe trait objects, for a selection of traits.
Statics§
- LIB_
HEADER - The header used to identify the version number of abi_stable that a dynamic libraries uses.
Traits§
- Interface
Type - Defines the usable/required traits when creating a
DynTrait<Pointer<()>, ThisInterfaceType>
. - Stable
Abi - Represents a type whose layout is stable.
Attribute Macros§
- export_
root_ module - This attribute is used for functions which export a module in an
implementation crate
. - sabi_
extern_ fn - The
sabi_extern_fn
attribute macro allows definingextern "C"
function that abort on unwind instead of causing undefined behavior. - sabi_
trait - This attribute generates an ffi-safe trait object on the trait it’s applied to.
Derive Macros§
- GetStatic
Equivalent - The
GetStaticEquivalent
macro derives theGetStaticEquivalent_
trait. - Stable
Abi - The StableAbi derive macro allows one to implement the
StableAbi trait
to :