pub async fn timeout_at<F, T, E>(
deadline: Instant,
future: F,
) -> Result<T, TimeoutError<E>>Available on crate feature
async only.Expand description
Applies a deadline to a Result-returning future.
Whether the deadline elapsed is indicated via the error type. Specifically:
- If
futuredoes not complete bydeadline, returnsTimeoutError::DeadlineElapsed. - If
futurecompletes withOk(t), returnsOk(t). - If
futurecompletes withErr(e), returnsTimeoutError::Inner(e).
Using this function can be considerably more readable than
tokio::time::timeout_at when the inner future returns a Result.
ยงExamples
use mz_ore::future::TimeoutError;
let slow_op = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok::<_, String>(())
};
let deadline = Instant::now() + Duration::from_millis(1);
let res = mz_ore::future::timeout_at(deadline, slow_op).await;
assert_eq!(res, Err(TimeoutError::DeadlineElapsed));