Skip to main content

timeout

Macro timeout 

Source
macro_rules! timeout {
    ($duration:expr, $future:expr) => { ... };
    ($cx:expr, $duration:expr, $future:expr) => { ... };
}
Expand description

Runs a future with a timeout.

This macro races the provided future against a sleep, returning the result if it completes in time, or an error if it times out.

§Semantics

let result = timeout!(Duration::from_secs(5), operation).await;

match result {
    Ok(value) => println!("Completed: {:?}", value),
    Err(Elapsed) => println!("Timed out"),
}

§Cancellation Behavior

When timeout fires:

  1. Main future is cancelled
  2. Cancellation follows standard protocol (drain + finalize)
  3. timeout! returns after main future is fully drained

When main future completes:

  1. Sleep is cancelled
  2. timeout! returns immediately (sleep cleanup is fast)