2019-08-14 14:14:44 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-08-27 09:47:15 +00:00
|
|
|
use futures_timer::TryFutureExt;
|
2019-08-14 14:14:44 +00:00
|
|
|
|
|
|
|
use crate::future::Future;
|
|
|
|
use crate::io;
|
|
|
|
|
|
|
|
/// Awaits an I/O future or times out after a duration of time.
|
|
|
|
///
|
2019-09-13 15:58:03 +00:00
|
|
|
/// If you want to await a non I/O future consider using
|
|
|
|
/// [`future::timeout`](../future/fn.timeout.html) instead.
|
|
|
|
///
|
2019-08-14 14:14:44 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
|
|
/// #
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// use async_std::io;
|
|
|
|
///
|
2019-08-15 15:59:48 +00:00
|
|
|
/// io::timeout(Duration::from_secs(5), async {
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut line = String::new();
|
|
|
|
/// let n = stdin.read_line(&mut line).await?;
|
2019-08-16 14:54:39 +00:00
|
|
|
/// Ok(())
|
2019-08-15 15:59:48 +00:00
|
|
|
/// })
|
|
|
|
/// .await?;
|
2019-08-14 14:14:44 +00:00
|
|
|
/// #
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
/// ```
|
|
|
|
pub async fn timeout<F, T>(dur: Duration, f: F) -> io::Result<T>
|
|
|
|
where
|
|
|
|
F: Future<Output = io::Result<T>>,
|
|
|
|
{
|
2019-08-27 09:47:15 +00:00
|
|
|
f.timeout(dur).await
|
2019-08-14 14:14:44 +00:00
|
|
|
}
|