Expand description
Rust-native declarations for importing and exporting C ABI interfaces.
ffi! is the main entry point. It generates ABI-facing wrappers and conversion glue for
statics, functions, impl blocks, and opaque types. Each block can either export declarations
from Rust with #![unsafe(export("ABI"))] or import them from a foreign library with
#![unsafe(extern("ABI"))].
Rust types used in those declarations are mapped to C-compatible representations through
ExternC. In normal use, derive ReprC to generate the representation and conversions.
For the complete syntax, safety contract, and conversion behavior, see the ffi! documentation.
§Example
use co3::{ReprC, ffi};
#[derive(Clone, Copy, ReprC)]
struct Step(u32);
ffi! {
#![unsafe(extern("C"))]
type Counter;
impl Counter {
#[symbol_name = "exported_by_int_inc"]
fn increment_by_int(&mut self, by: u32);
#[symbol_name = "exported_custom_inc"]
fn increment_custom(&mut self, by: Step);
}
}Modules§
- borrow
- boxed
- Owning C-ABI carrier types.
- cell
- Raw access to a value stored behind interior mutability.
- niche
- Logic related to the conversion of
Option<T>to and from FFI-compatible representation - option
- C-compatible representation of
core::option::Option. - result
- C-compatible representation of
core::result::Result. - slice
- C-ABI slice carriers.
- stored
- tag
- Utilities for opaque pointer handles required for tagged dispatch.
- transmute
- tuple
- Provides
repr(C)tuple types that can be safely passed across FFI boundaries. - wide
Macros§
- ffi
- Declare FFI exports or imports via native API.
- impls
- Returns
trueif a type implements a logical trait expression.
Traits§
- CFnArg
ReprCtype that is allowed as a C function argument.- CFnReturn
ReprCtype that is allowed as a C function return value.- Decode
- Facilitates conversion into a Rust type from a corresponding C-compatible representation.
- Encode
- Facilitates conversion from a Rust type into a corresponding C-compatible representation.
- Error
- ExternC
- A Rust type that has an
extern "C"ABI - ReprC
- Robust type that conforms to C ABI and can be safely shared across FFI boundaries.
Functions§
- decode⚠
- Perform the conversion from
T::CTypeintoT. - encode
- Perform the conversion from
TintoExternC::CType. - soft_
decode ⚠ - Perform the conversion from
T::CTypeintoTusing external storage. - soft_
encode - Perform the conversion from
TintoExternC::CTypeusing external storage.