[][src]Crate abi_stable

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:

  • ffi-safe equivalent of trait objects for any combination of a selection of traits.

  • Provides ffi-safe alternatives to many standard library types..

  • Provides the StableAbi trait for asserting that types are ffi-safe.

  • Features 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.

  • Provides the StableAbi derive macro to both assert that the type is ffi compatible, and to get the layout of the type at runtime to check that it is still compatible.

Examples

For examples of using abi_stable you can look at the crates in the examples directory , in the repository for this crate.

To run the examples generally you'll have to build the *_impl crate, then run the *_user crate (all *_user crates should have a help message and a readme.md).

Glossary

interface crate:the crate that declares the public functions and types 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, and implement the ModuleTrait trait. These are declared in the interface crate,exported in the implementation crate, and loaded in the user crate.

Rust-to-Rust FFI guidelines.

Types must implement StableAbi to be safely passed through the FFI boundary, which can be done using the StableAbi derive macro.

These are the kinds of types passed through FFI:

  • Value kind: The layout of types passed by value must not change in a minor version. This is the default kind when deriving StableAbi.

  • Opaque kind: Types wrapped in VirtualWrapper<SomePointer<ZeroSized<Interface>>>, 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 kind: Types only accessible through shared references, most commonly vtables and modules, which can be extended in minor versions while staying ABI compatible. by adding fields at the end.

Declaring enums

Adding variants or fields to a variant is disallowed in minor versions.

To represent non-exhaustive enums without fields it is recommended using structs and associated constants so that it is not UB to keep adding field-less variants in minor versions.

Extra documentation

Macros

Re-exports

pub use abi_stable_derive::StableAbi;
pub use abi_stable_derive::export_sabi_module;
pub use abi_stable_derive::impl_InterfaceType;

Modules

abi_stability

types and traits related to abi stability.

docs

Documentation for macros,and guides.

erased_types

Types and traits related to type erasure.

ignored_wrapper

Wrapper type(s) where their value is ignored in some trait impls .

lazy_static_ref

A late-initialized static reference.

library

Traits and types related to loading an abi_stable dynamic library, as well as functions/modules within.

marker_type

Zero-sized types .

pointer_trait

Traits for pointers.

prefix_type

Types,traits,and functions used by prefix-types.

reexports

Miscelaneous items re-exported from core_extensions.

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_level

Types used to represent values at compile-time,eg:True/False.

utils

Utility functions.

version

Types representing the version number of a library.

Macros

extern_fn_panic_handling

Use this to make sure that you handle panics inside extern fn correctly.

impl_get_type_info

Implements the abi_stable::type_info::GetTypeInfo trait for some type.

package_version_strings

Instantiates a abi_stable::version::VersionStrings with the major.minor.patch version of the library where it is invoked.

rtry

Equivalent to ? for RResult<_,_>.

rtry_opt

Equivalent to ? for ROption<_>.

tl_genparams

Use this when constructing a abi_stable::abi_stability::TypeLayoutParams when manually implementing StableAbi, to more ergonomically initialize the generics field.

Structs

VirtualWrapper

VirtualWrapper implements ffi-safe trait objects,for a selection of traits.

ZeroSized

A Zero-sized type used by VirtualWrapper<Pointer<ZeroSized<T>>>.

Traits

ImplType

An implementation type, with an associated interface type which describes the traits that must be implemented when constructing a VirtualWrapper from Self, using the from_value and from_ptr constructors, so as to pass an opaque type across ffi.

InterfaceType

Defines the usable/required traits when creating a VirtualWrapper<Pointer<ZeroSized< ThisType >>> from a type that implements ImplType<Interface= ThisType > .

StableAbi

Represents a type whose layout is stable.

Type Definitions

ErasedObject

Used by vtables/pointers to signal that the type has been erased.