Skip to content

Commit 8de9f9b

Browse files
committed
Merge branch 'future-timeout' of https://github.com/miker1423/async-std into future-timeout
2 parents 1c2055f + 33e7c87 commit 8de9f9b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/future/future/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,25 @@ extension_trait! {
360360
#[doc = r#"
361361
Waits for both the future and a timeout, if the timeout completes before
362362
the future, it returns an TimeoutError.
363+
364+
# Example
365+
```
366+
#async_std::task::block_on(async {
367+
let fut = future::ready(0);
368+
let dur = Duration::from_millis(100);
369+
let res = fut.timeout(dur).await;
370+
assert!(res.is_ok());
371+
372+
let fut = future::ready(0);
373+
let dur = Duration::from_millis(100);
374+
let res = fut.timeout(dur).await;
375+
assert!(res.is_ok())
376+
# });
377+
```
363378
"#]
364379
#[cfg(any(feature = "unstable", feature = "docs"))]
365380
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
366-
fn timeout<F, T>(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
381+
fn timeout(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
367382
where Self: Sized
368383
{
369384
TimeoutFuture::new(self, dur)

src/future/timeout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ pin_project! {
5151
}
5252

5353
impl<F> TimeoutFuture<F> {
54-
pub fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
54+
#[allow(dead_code)]
55+
pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
5556
TimeoutFuture { future: future, delay: Delay::new(dur) }
5657
}
5758
}

tests/timeout_future.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![cfg(feature = "unstable")]
2+
3+
use std::time::Duration;
4+
5+
use async_std::future;
6+
use async_std::prelude::*;
7+
use async_std::task;
8+
9+
#[test]
10+
fn should_timeout() {
11+
task::block_on(async {
12+
let fut = future::pending::<()>();
13+
let dur = Duration::from_millis(100);
14+
let res = fut.timeout(dur).await;
15+
assert!(res.is_err());
16+
});
17+
}
18+
19+
#[test]
20+
fn should_not_timeout() {
21+
task::block_on(async {
22+
let fut = future::ready(0);
23+
let dur = Duration::from_millis(100);
24+
let res = fut.timeout(dur).await;
25+
assert!(res.is_ok());
26+
});
27+
}

0 commit comments

Comments
 (0)