[][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:

Examples

For examples of using abi_stable you can look at the readme, or for the crates in the examples directory in the repository for this crate. This crate also has examples for most features on their own.

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. The root module implement 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 look here.

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.

  • Nonexhaustive enums:
    Enums wrapped inside NonExhaustive<>, which can add variants in minor versions of the library.

  • Opaque kind:
    Types wrapped in DynTrait<SomePointer<()>,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.

  • Trait objects:
    Trait object-like types generated using #[sabi_trait], which erase the type of the value they wrap,implements the methods of the trait, and can be unwrapped back to the original type in the dynamic library/binary that created it (if it was constructed to be unerasable and implements Any).

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

Extra documentation

Macros (derive and attribute)

Modules

abi_stability

types and traits related to abi stability.

const_utils

Utilities for const contexts.

docs

Documentation for macros,and guides.

erased_types

Types and traits related to type erasure.

external_types

Ffi wrapper for types defined outside the standard library.

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.

impl_get_type_info

Constructs the abi_stable::erased_types::TypeInfo for some type.

make_item_info

Constructs a abi_stable::type_layout::ItemInfo, with information about the place where it's called.

nul_str

Constructs a NulStr from a string literal.

package_version_strings

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

rslice

A macro to construct RSlice<'_,T> constants.

rstr

A macro to construct RStr<'static> constants.

rtry

Equivalent to ? for RResult<_,_>.

rtry_opt

Equivalent to ? for ROption<_>.

rtuple

Use this macro to construct a Tuple* with the values passed to the macro.

rvec

Constructs an RVec<_> using the same syntax that the std::vec macro uses.

tag

Constructs a abi_stable::abi_stability::Tag, a dynamically typed value for users to check extra properties about their types when doing runtime type checking.

tl_genparams

Use this when constructing a abi_stable::type_layout::MonoTypeLayout when manually implementing StableAbi.

type_identity

Allows converting between Copy generic types that are the same concrete type (using AssertEq to prove that they are).

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

ImplType

An implementation type, with an associated interface type which describes the traits that must be implemented when constructing a DynTrait 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 DynTrait<Pointer<()>,ThisInterfaceType> from a type that implements ImplType<Interface= ThisInterfaceType > .

StableAbi

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

This macro is documented in abi_stable::docs::sabi_extern_fn

sabi_trait

This macro is documented in abi_stable::docs::sabi_trait_attribute .

Derive Macros

GetStaticEquivalent

This macro is documented in abi_stable::docs::get_static_equivalent

StableAbi

This macro is documented in abi_stable::docs::stable_abi_derive