Function cros_asyncv2::with_deadline
source · [−]Expand description
Add a deadline to an asynchronous operation.
Returns the output of the asynchronous operation if it completes before the deadline
expires. Otherwise returns a TimedOut
error.
If the deadline expires before the asynchronous operation completes then f
is dropped.
However, this may not cancel any underlying asynchronous I/O operations registered with the OS.
Examples
Set a timeout for reading from a data source.
use std::time::{Duration, Instant};
use cros_async::{with_deadline, File};
async fn read_with_timeout(
rx: &File,
buf: &mut [u8],
timeout: Duration,
) -> anyhow::Result<usize> {
with_deadline(Instant::now() + timeout, rx.read(buf, None)).await?
}