Skip to main content

Crate weaveffi

Crate weaveffi 

Source
Expand description

WeaveFFI: write safe Rust, get a stable C ABI and bindings for 11 languages.

This is the single crate a Rust producer depends on. Annotate an ordinary module with module, tag the items you want to export, and call export_runtime! once. The module expansion emits the #[no_mangle] extern "C" thunks that the generated language bindings call, marshalling every argument and result through the audited abi runtime so you never write unsafe glue by hand.

#[weaveffi::module]
pub mod calculator {
    /// Add two integers.
    #[weaveffi::export]
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    /// Divide, reporting division by zero through the ABI's error channel.
    #[weaveffi::export]
    pub fn div(a: i32, b: i32) -> Result<i32, String> {
        if b == 0 {
            return Err("division by zero".to_string());
        }
        Ok(a / b)
    }
}

// Expose the fixed runtime surface (memory/error/cancel helpers) once.
weaveffi::export_runtime!();

The same annotated source is what weaveffi generate path/to/lib.rs reads to emit the IDL, header, and bindings, so the producer and the bindings cannot drift: they are two views of one parse.

§What you get

  • module - the driver attribute on an exported mod.
  • export - export a function (async fn is asynchronous; a Result-returning fn is fallible).
  • record - a by-value struct with generated create/getters.
  • enumeration - a #[repr(i32)] C-style enum.
  • callback / listener - a callback and an event listener.
  • cancellable - mark an async fn as accepting a cancel token; builder - opt a record into a fluent builder.
  • abi - the C ABI runtime: the error struct, memory helpers, the marshalling converters the expansion calls, and export_runtime!.

Re-exports§

pub use weaveffi_abi as abi;

Macros§

export_runtime
Emit #[no_mangle] extern "C" thunks for every runtime symbol that the WeaveFFI generators expect to find in the consuming cdylib.

Structs§

CancelToken
A Send view of a foreign cancellation token, accepted as the final parameter of a #[weaveffi::cancellable] async fn. Poll is_cancelled at safe points and return early when it reports cancellation; the module expansion supplies the token from the async launcher’s cancel_token slot. A safe, Send view of a foreign weaveffi_cancel_token handed to a cancellable async fn.
Iter
An owned, lazily-pulled iterator returned by a producer function whose IDL return type is iter<T>. Construct one from any iterator with Iter::new; the module expansion turns it into the opaque iterator handle the generated bindings consume. An owned, type-erased iterator handed across the C ABI boundary.

Traits§

ErrorReport
Maps a producer error onto the ABI’s (code, message) pair. A fallible #[weaveffi::export] function reports Err(e) through its trailing out_err slot using this trait, so every std::fmt::Display error gets the generic code -1, while a type that implements ErrorReport directly surfaces the named codes of an IDL error domain. Maps a producer error onto the ABI’s (code, message) pair.

Attribute Macros§

builder
Opt a record into a generated fluent builder.
callback
Declare a callback function signature the host implements.
cancellable
Mark an async function as accepting a cancellation token.
enumeration
Declare a C-style #[repr(i32)] enum exported by value.
error
Declare the module’s error domain from a unit-variant enum with explicit discriminants. The module macro generates the matching ErrorReport implementation.
export
Export a function across the FFI boundary. An async fn lowers to an asynchronous symbol; a fn -> Result<T, E> is fallible.
interface
Declare an interface: an opaque object type with constructors, methods, and statics read from its impl block. Methods must take &self.
listener
Declare an event listener; takes event = "CallbackName".
module
Mark an inline mod as an exported WeaveFFI namespace.
record
Declare a by-value record (struct) with generated create/getters.