Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Runtime support

The generated code calls a few items in psync::__private (src/graph_rt.rs). They are not a public API. The macro’s output is the only intended caller, and they may change freely. Everything here uses only core and alloc.

ItemUsed for
maybe_done(future) -> MaybeDone<Pin<Box<F>>>one branch of a level’s join
MaybeDone::poll_step(cx) -> boolpoll if still pending; true once the output is available
MaybeDone::take()take the output (panics if called before completion, which generated code never does)
join_all_bounded(iter, bound)a prefetched loop: run at most bound futures, outputs in input order
over(&iter, f)type the async closure’s parameter from the iterator
maybe_done_at::<LEVEL, ORDER, LAST, EXITS, EXIT_AT, SPEC, INDEP, _>, job_at::<..> (synchronous jobs), over_at::<LOOP, TERMINATES, SPEC, INDEP, ..>what generated code calls: the same helpers, tagged with the macro’s assumptions for cargo psync check; free at run time
Branch, Rethrow, ResultResidual, OptionResidualrecord ? and replay it through the caller’s ?
Vecre-exported so generated code can name alloc::vec::Vec
tick(&counter, every) -> bool, yield_now()yield_every = N: count a loop’s iterations in a counter static to that loop, and yield to the executor on every Nth

MaybeDone

Each branch is boxed with Box::pin. That lets the join poll it through Pin::new without unsafe pin projection. It costs one allocation per branch per level, which is small next to an I/O round trip. A version without allocation would need pin projection in generated code.

join_all_bounded

pub struct JoinAllBounded<I: Iterator> where I::Item: Future {
    iter: Option<Box<I>>,
    active: Vec<(usize, Pin<Box<I::Item>>)>,
    results: Vec<Option<<I::Item as Future>::Output>>,
    bound: usize,
}

On each poll it:

  1. starts new items while fewer than bound are active, so a freed slot is refilled at once;
  2. polls every active future, and stores finished outputs at their input index;
  3. returns all outputs, in input order, when the iterator is exhausted and nothing is active;
  4. returns Pending if nothing finished during this pass. Nothing new can start until a wake.

It does not stop early on errors. Errors are values recorded in exit slots, and the loop body replays them in order. Every item’s future is therefore run to completion. This is the speculation described in Loops.

Generated async code names core or #krate::__private, so it compiles in no_std crates that have alloc (the RP2350 build is one). Fission’s first loop collects into the re-exported Vec. Three pieces name std, and each is generated only for code that already needs it: the comparison of file paths at run time (std::path, for a function that uses std::fs or tokio::fs), the per-item file check of a loop (std::collections::HashMap and std::path), and the replay of a panic in synchronous #[psync::par] code (std::panic::resume_unwind, which needs threads).

Exercise

A prefetched loop runs four items with bound = 2. Item 0 takes 3T, and items 1, 2 and 3 take T each. Item 1 fails. How long does the join take, in what order are the outputs, and do items 2 and 3 still run? How long would it take if the join started items in batches of two?

Answer

3T. Item 0 holds one slot the whole time, and items 1, 2 and 3 take turns in the other, each starting in the same poll in which the one before it finishes. The outputs come back in input order, [Ok(0), Err("one"), Ok(2), Ok(3)], and items 2 and 3 do run: an error is a value, which the loop body replays in order afterwards. Batches of two would take 4T: (3T, T), then (T, T). Test book_bounded_join_refills_slots in tests/graph.rs.