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 exportedmod.export- export a function (async fnis asynchronous; aResult-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 anasync fnas 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, andexport_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§
- Cancel
Token - A
Sendview of a foreign cancellation token, accepted as the final parameter of a#[weaveffi::cancellable]async fn. Pollis_cancelledat safe points and return early when it reports cancellation; themoduleexpansion supplies the token from the async launcher’scancel_tokenslot. A safe,Sendview of a foreignweaveffi_cancel_tokenhanded to a cancellableasync fn. - Iter
- An owned, lazily-pulled iterator returned by a producer function whose IDL
return type is
iter<T>. Construct one from any iterator withIter::new; themoduleexpansion turns it into the opaque iterator handle the generated bindings consume. An owned, type-erased iterator handed across the C ABI boundary.
Traits§
- Error
Report - Maps a producer error onto the ABI’s
(code, message)pair. A fallible#[weaveffi::export]function reportsErr(e)through its trailingout_errslot using this trait, so everystd::fmt::Displayerror gets the generic code-1, while a type that implementsErrorReportdirectly 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
ErrorReportimplementation. - export
- Export a function across the FFI boundary. An
async fnlowers to an asynchronous symbol; afn -> Result<T, E>is fallible. - interface
- Declare an interface: an opaque object type with constructors, methods,
and statics read from its
implblock. Methods must take&self. - listener
- Declare an event listener; takes
event = "CallbackName". - module
- Mark an inline
modas an exported WeaveFFI namespace. - record
- Declare a by-value record (struct) with generated create/getters.