forked from mirror/async-std
Merge branch 'future-timeout' of https://github.com/miker1423/async-std into future-timeout
This commit is contained in:
commit
8de9f9b8e1
3 changed files with 45 additions and 2 deletions
|
@ -360,10 +360,25 @@ extension_trait! {
|
|||
#[doc = r#"
|
||||
Waits for both the future and a timeout, if the timeout completes before
|
||||
the future, it returns an TimeoutError.
|
||||
|
||||
# Example
|
||||
```
|
||||
#async_std::task::block_on(async {
|
||||
let fut = future::ready(0);
|
||||
let dur = Duration::from_millis(100);
|
||||
let res = fut.timeout(dur).await;
|
||||
assert!(res.is_ok());
|
||||
|
||||
let fut = future::ready(0);
|
||||
let dur = Duration::from_millis(100);
|
||||
let res = fut.timeout(dur).await;
|
||||
assert!(res.is_ok())
|
||||
# });
|
||||
```
|
||||
"#]
|
||||
#[cfg(any(feature = "unstable", feature = "docs"))]
|
||||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||
fn timeout<F, T>(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
|
||||
fn timeout(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
|
||||
where Self: Sized
|
||||
{
|
||||
TimeoutFuture::new(self, dur)
|
||||
|
|
|
@ -51,7 +51,8 @@ pin_project! {
|
|||
}
|
||||
|
||||
impl<F> TimeoutFuture<F> {
|
||||
pub fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
|
||||
TimeoutFuture { future: future, delay: Delay::new(dur) }
|
||||
}
|
||||
}
|
||||
|
|
27
tests/timeout_future.rs
Normal file
27
tests/timeout_future.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
#![cfg(feature = "unstable")]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use async_std::future;
|
||||
use async_std::prelude::*;
|
||||
use async_std::task;
|
||||
|
||||
#[test]
|
||||
fn should_timeout() {
|
||||
task::block_on(async {
|
||||
let fut = future::pending::<()>();
|
||||
let dur = Duration::from_millis(100);
|
||||
let res = fut.timeout(dur).await;
|
||||
assert!(res.is_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_timeout() {
|
||||
task::block_on(async {
|
||||
let fut = future::ready(0);
|
||||
let dur = Duration::from_millis(100);
|
||||
let res = fut.timeout(dur).await;
|
||||
assert!(res.is_ok());
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue