pub fn timeout_at<F>(deadline: Instant, future: F) -> Timeout<F::IntoFuture> ⓘwhere
F: IntoFuture,time only.Expand description
Requires a Future to complete before the specified instant in time.
If the future completes before the instant is reached, then the completed value is returned. Otherwise, an error is returned.
This function returns a future whose return type is Result<T,Elapsed>, where T is the
return type of the provided future.
If the provided future completes immediately, then the future returned from
this function is guaranteed to complete immediately with an Ok variant
no matter the provided deadline.
§Cancellation
Cancelling a timeout is done by dropping the future. No additional cleanup or other work is required.
The original future may be obtained by calling Timeout::into_inner. This
consumes the Timeout.
§Examples
Create a new Timeout set to expire in 10 milliseconds.
use tokio::time::{Instant, timeout_at};
use tokio::sync::oneshot;
use std::time::Duration;
let (tx, rx) = oneshot::channel();
// Wrap the future with a `Timeout` set to expire 10 milliseconds into the
// future.
if let Err(_) = timeout_at(Instant::now() + Duration::from_millis(10), rx).await {
println!("did not receive value within 10 ms");
}§Panics
This function panics if there is no current timer set.
It can be triggered when Builder::enable_time or
Builder::enable_all are not included in the builder.
It can also panic whenever a timer is created outside of a
Tokio runtime. That is why rt.block_on(sleep(...)) will panic,
since the function is executed outside of the runtime.
Whereas rt.block_on(async {sleep(...).await}) doesn’t panic.
And this is because wrapping the function on an async makes it lazy,
and so gets executed inside the runtime successfully without
panicking.