Skip to main content

Module rust

Module rust 

Source
Expand description

Extract the IR of a #[weaveffi::module] tree from its Rust syntax.

This is the #[weaveffi::module] proc-macro’s reader: it lowers a module tree to its IR (extract_module), which the macro validates, builds the C ABI scaffolding from, and embeds in the library as metadata (see meta). The CLI never reads Rust source; it reads that metadata back out of the built library, so the API it generates bindings for is, by construction, the one the library was compiled with.

The extraction reports only what syntax alone rules out (a raw pointer, a tuple variant, a #[cfg] on a member); every other rule is the validator’s. Alongside the IR, extract_module returns a SourceMap recording where each declaration came from and the #[cfg] attributes on it, so the macro can point a validation error at the offending item and apply each item’s #[cfg] to its generated code.

§The annotation scheme

A #[weaveffi::module] on an inline mod marks an exported namespace. Inside it:

  • #[weaveffi::export] on a fn exports a function. An async fn lowers to an asynchronous symbol; a fn -> Result<T, E> is fallible (throws in the IDL; the return type is T and errors are reported through the ABI’s out_err).
  • #[weaveffi::interface] on a struct declares an interface (opaque, reference-counted object type). Its impl block’s pub fns become the interface’s members: an associated function returning Self (or Arc<Self>) is a constructor, a &self or self: Arc<Self> function is a method, and any other associated function is a static.
  • #[weaveffi::record] on a struct declares a by-value record.
  • #[weaveffi::enumeration] on a #[repr(i32)] enum declares a C-style enum; an enum with data-carrying variants declares a rich enum.
  • #[weaveffi::error] on an enum declares the module’s error domain.
  • #[weaveffi::callback_interface] on a trait declares a callback interface: a method set the consumer implements. Producers accept one as Arc<dyn Trait> (or Option<Arc<dyn Trait>>). Every method returns Result<T, weaveffi::ForeignError>, which maps to a T return; a method marked #[weaveffi::throws] throws.
  • pub type Name = T; in the tree is an alias: every use of Name is extracted as T.

The type mapping mirrors the IDL: String (or &str) is a string, Vec<u8> (or &[u8]) is a byte buffer, every integer primitive (including u64) is the matching scalar, Vec<T>, Option<T>, and HashMap/BTreeMap are the list, optional, and map types, weaveffi::Iter<T> is an iter<T> return, Arc<T> names the interface T (an object reference), Arc<dyn Trait> names the callback interface Trait, and any other named path is a record, enum, or interface resolved later.

Structs§

SourceMap
Where each extracted declaration came from, keyed by its declaration path: module segments from the tree’s root, then the declaration name, then a member name (an interface’s constructor, method, or static; a callback interface’s method; an enum’s variant; an error domain’s code), then a parameter or field name. These are the paths validation reports its errors under.

Functions§

extract_module
Extract the module tree rooted at a #[weaveffi::module]-annotated mod, together with its SourceMap.
has_marker
Whether any attribute in attrs is the WeaveFFI marker name.
output_is_result
Whether a function’s return type is a Result<_, _>, which marks the function as throws in the IDL.
peel_arc
Peel Arc<T> to its T, returning any other type unchanged.
peel_result
Peel Result<T, E> to its T, returning any other type unchanged. A Result return marks the function throws in the IDL; the error type itself carries no extra IR shape (it reports through the ABI’s out_err).
receiver_is_supported
Whether a method receiver is one WeaveFFI can lift: &self (a borrow for the call) or self: Arc<Self> (a retained reference).
returns_foreign_result
Whether a return type is Result<T, ForeignError>, the return type every callback-interface method must have.