From 54fa559554d1374f4a9c40da90452cb8366d0aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 5 Dec 2019 08:09:20 -0600 Subject: [PATCH 1/5] Changing scope of disclosure --- src/future/timeout.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/future/timeout.rs b/src/future/timeout.rs index c2b6306..05aaa45 100644 --- a/src/future/timeout.rs +++ b/src/future/timeout.rs @@ -51,7 +51,8 @@ pin_project! { } impl TimeoutFuture { - pub fn new(future: F, dur: Duration) -> TimeoutFuture { + #[allow(dead_code)] + pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture { TimeoutFuture { future: future, delay: Delay::new(dur) } } } From c14c377974c6bdd17e6a118eb061b33bdac63bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 5 Dec 2019 08:09:58 -0600 Subject: [PATCH 2/5] Changing method signature --- src/future/future/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index fc800cb..569d032 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -363,7 +363,7 @@ extension_trait! { "#] #[cfg(any(feature = "unstable", feature = "docs"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] - fn timeout(self, dur: Duration) -> impl Future [TimeoutFuture] + fn timeout(self, dur: Duration) -> impl Future [TimeoutFuture] where Self: Sized { TimeoutFuture::new(self, dur) From 4670388a568b39f13d15c4d1f03bb15282ff3143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 5 Dec 2019 08:10:06 -0600 Subject: [PATCH 3/5] Adding tests --- tests/timeout_future.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/timeout_future.rs diff --git a/tests/timeout_future.rs b/tests/timeout_future.rs new file mode 100644 index 0000000..e1dfb4e --- /dev/null +++ b/tests/timeout_future.rs @@ -0,0 +1,27 @@ +#![cfg(feature = "unstable")] + +use std::time::Duration; + +use async_std::prelude::*; +use async_std::future; +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()); + }); +} \ No newline at end of file From cc85533f7c50b9c15fdaac840001c057cb73b734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 5 Dec 2019 21:15:32 -0600 Subject: [PATCH 4/5] fixing format --- tests/timeout_future.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/timeout_future.rs b/tests/timeout_future.rs index e1dfb4e..e6e4b34 100644 --- a/tests/timeout_future.rs +++ b/tests/timeout_future.rs @@ -2,13 +2,13 @@ use std::time::Duration; -use async_std::prelude::*; use async_std::future; +use async_std::prelude::*; use async_std::task; #[test] fn should_timeout() { - task::block_on(async { + task::block_on(async { let fut = future::pending::<()>(); let dur = Duration::from_millis(100); let res = fut.timeout(dur).await; @@ -18,10 +18,10 @@ fn should_timeout() { #[test] fn should_not_timeout() { - task::block_on(async { + 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()); }); -} \ No newline at end of file +} From 33e7c87dfc6dba1c22080f3bfa334830c667ef2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 5 Dec 2019 21:19:02 -0600 Subject: [PATCH 5/5] Adding example to docs --- src/future/future/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index 569d032..efa832a 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -360,6 +360,21 @@ 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)))]