select_loop

Macro select_loop 

Source
select_loop!() { /* proc-macro */ }
Expand description

Convenience macro to continuously select! over a set of futures in biased order, with a required shutdown handler.

This macro automatically creates a shutdown future from the provided context and requires a shutdown handler block. The shutdown future is created outside the loop, allowing it to persist across iterations until shutdown is signaled. The shutdown branch is always checked first (biased).

After the shutdown block is executed, the loop breaks by default. If different control flow is desired (such as returning from the enclosing function), it must be handled explicitly.

§Syntax

select_loop! {
    context,
    on_stopped => { cleanup },
    pattern = future => block,
    // ...
}

The shutdown variable (the future from context.stopped()) is accessible in the shutdown block, allowing explicit cleanup such as drop(shutdown) before breaking or returning.

§Example

use commonware_macros::select_loop;

async fn run(context: impl commonware_runtime::Spawner) {
    select_loop! {
        context,
        on_stopped => {
            println!("shutting down");
            drop(shutdown);
        },
        msg = receiver.recv() => {
            println!("received: {:?}", msg);
        },
    }
}