Skip to main content

alloc_pair_chain

Function alloc_pair_chain 

Source
pub async fn alloc_pair_chain<T>(
    caller: &mut Caller<'_, T>,
    items: impl IntoIterator<Item = Rooted<AnyRef>>,
) -> Result<Option<Rooted<StructRef>>>
where T: Send,
Expand description

Folds an iterator of GC-ref elements into a $pair chain by re-entering the wasm module via its exported pair_new function. Returns the chain head, or None if the iterator is empty.

$pair is the self-recursive cell type ({anyref car, ref null $pair cdr}) declared by CompileContext::new_skeleton. The host can’t freshly construct that StructType via StructType::new — the cdr field references the type itself, which StructType::new doesn’t model. Instead this helper reaches into the module’s own type system: pair_new is already emitted by the compiler skeleton as register_function("pair_new", ...) which adds it to the export table, so the host fn body can pull it from Caller::get_export and invoke it per element. Each call produces a Rooted<StructRef> of the exact $pair type that subsequent ref.cast (ref null $pair) operations in the guest accept.

Items are folded right-to-left so the first element of the iterator ends up at the chain head — [a, b, c](a . (b . (c . nil))).