Skip to main content

Crate co3

Crate co3 

Source
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 true if a type implements a logical trait expression.

Traits§

CFnArg
ReprC type that is allowed as a C function argument.
CFnReturn
ReprC type 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::CType into T.
encode
Perform the conversion from T into ExternC::CType.
soft_decode
Perform the conversion from T::CType into T using external storage.
soft_encode
Perform the conversion from T into ExternC::CType using external storage.

Derive Macros§

ReprC
Generate a C-compatible counterpart and conversions to and from the Rust type.
Tag
Derives the tagged-dispatch traits for a Rust type.