Function mz_ore::future::timeout_at
source · 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
future
does not complete bydeadline
, returnsTimeoutError::DeadlineElapsed
. - If
future
completes withOk(t)
, returnsOk(t)
. - If
future
completes 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));