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 afnexports a function. Anasync fnlowers to an asynchronous symbol; afn -> Result<T, E>is fallible (throwsin the IDL; the return type isTand errors are reported through the ABI’sout_err).#[weaveffi::interface]on astructdeclares an interface (opaque, reference-counted object type). Itsimplblock’spub fns become the interface’s members: an associated function returningSelf(orArc<Self>) is a constructor, a&selforself: Arc<Self>function is a method, and any other associated function is a static.#[weaveffi::record]on astructdeclares a by-value record.#[weaveffi::enumeration]on a#[repr(i32)]enumdeclares 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 atraitdeclares a callback interface: a method set the consumer implements. Producers accept one asArc<dyn Trait>(orOption<Arc<dyn Trait>>). Every method returnsResult<T, weaveffi::ForeignError>, which maps to aTreturn; a method marked#[weaveffi::throws]throws.pub type Name = T;in the tree is an alias: every use ofNameis extracted asT.
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§
- Source
Map - 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]-annotatedmod, together with itsSourceMap. - has_
marker - Whether any attribute in
attrsis the WeaveFFI markername. - output_
is_ result - Whether a function’s return type is a
Result<_, _>, which marks the function asthrowsin the IDL. - peel_
arc - Peel
Arc<T>to itsT, returning any other type unchanged. - peel_
result - Peel
Result<T, E>to itsT, returning any other type unchanged. AResultreturn marks the functionthrowsin the IDL; the error type itself carries no extra IR shape (it reports through the ABI’sout_err). - receiver_
is_ supported - Whether a method receiver is one WeaveFFI can lift:
&self(a borrow for the call) orself: 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.