Async timeout Option

I'm looking for something akin to an Option that supports waiting for its inner object if Some, or wait indefinitely if None, to be used to implement an optional timeout.

Conceptually:

let timeout = async_timeout(Duration::from_secs(60));
let timeout = AsyncOption::Some(timeout);
loop {
  tokio::select!{
    _ = timeout => {
      // A timeout happened!  This may never happen (in the case a
      // timeout was set to AsyncOption::None).
    }
    msg = rx.recv() => {
      // A message was received
    }
    _ = terminate => {
      break
    }
  }
}

Usually the easiest way to do this is to write a helper function:

async fn helper(timeout: &mut Option<Timeout>) {
    match timeout { ... }
}
1 Like