Expand description
Consumer-implemented callback interfaces: the producer half of the ABI’s vtable contract.
A callback-interface parameter arrives as two slots, a void* ctx the
consumer owns and a pointer to a process-wide static vtable. Every vtable
starts with the same header (VtableHeader: its size, reserved
flags, and the free(ctx) release hook), followed by one extern "C"
function pointer per method. The #[weaveffi::module] expansion wraps
the pair in a ForeignCallback, implements the producer’s trait on top
of it (each method lowers its arguments, calls the vtable entry, adopts
the return, then checks out_err), and hands the producer an
Arc<dyn Trait>. When the last Arc drops, ForeignCallback calls
free(ctx) exactly once.
The producer rejects a vtable whose size is smaller than the vtable it
was built with, so a consumer generated from an older contract can’t make
the producer call through a slot that doesn’t exist. A larger size is
accepted, which lets a newer consumer pass methods this producer doesn’t
know yet.
A callback method returns Result<T, ForeignError>. A failure the
consumer reports through out_err becomes the Err: a method that
throws keeps a declared code of its module’s error domain (decode it
with ForeignError::domain), and every other failure has the code
FOREIGN_ERROR_CODE.
Structs§
- Foreign
Callback - A consumer-implemented callback interface held by the producer.
- Foreign
Error - A failure a consumer’s callback-interface implementation reported, or a value it returned that the producer couldn’t accept.
- Vtable
Header - The fixed header every callback-interface vtable starts with, in this
order and at these offsets (
{prefix}_..._vtablein C). - Vtable
TooSmall - A vtable’s
sizeis smaller than the vtable the producer was built with.
Traits§
- Callback
Interface - Ties a callback interface’s
dyn Traitto its generated vtable and foreign wrapper. - Vtable
- Implemented by every generated
#[repr(C)]vtable struct, whose first field is itsVtableHeader.
Functions§
- callback_
ret_ ⚠buffer - Adopt and decode a value buffer a vtable entry wrote to its
out_ptr/out_lenslots. - callback_
ret_ ⚠bytes - Adopt the byte run a vtable entry wrote to its
out_ptr/out_lenslots. - callback_
ret_ enum - Lift a C-style enum a vtable entry returned.
- callback_
ret_ ⚠object - Adopt the strong object reference a vtable entry returned.
- callback_
ret_ ⚠object_ opt - Adopt the optional object reference a vtable entry returned (
I?). - callback_
ret_ ⚠string - Adopt a string a vtable entry wrote to its
out_ptr/out_lenslots. - callback_
status - Read the
out_errslot a vtable entry of a method that doesn’tthrowwrote:Ok(())when the consumer succeeded, otherwise its failure with the codeFOREIGN_ERROR_CODE(whatever code it wrote). - callback_
status_ in - Read the
out_errslot a vtable entry of athrowsmethod wrote: a positive code thatEdeclares (with a payload that decodes) is kept, and every other failure gets the codeFOREIGN_ERROR_CODE. - lift_
callback ⚠ - Lift a callback-interface parameter’s
(ctx, vtable)slots into theArc<dyn Trait>the producer’s function takes.nameis the parameter’s name, for the error message. - lift_
callback_ ⚠opt - Lift an optional callback-interface parameter (
Cb?): a null vtable isNone.