Skip to main content

race

Macro race 

Source
macro_rules! race {
    (biased; $($future:expr),+ $(,)?) => { ... };
    ($($future:expr),+ $(,)?) => { ... };
}
Expand description

Macro for racing multiple futures.

The first future to complete wins.

Note: this macro is currently a placeholder and does not implement the full asupersync race semantics (cancel + drain losers). Use the Scope APIs (Scope::race, Scope::race_all) when racing spawned tasks.

§Basic Usage

let winner: Race2<A, B> = race!(fut_a, fut_b).await;
let winner: Race3<A, B, C> = race!(fut_a, fut_b, fut_c).await;

§Biased Mode

Use biased; for left-to-right polling priority (useful for fallback patterns):

race! { biased;
    check_cache(key),
    query_database(key),
}

§Key Properties

  1. First future to return Poll::Ready is the winner
  2. All non-winning futures go through the cancellation protocol
  3. race! waits for all losers to complete before returning
  4. Losers complete with Outcome::Cancelled(RaceLost)