From c1f7be5d42f9fbeb63250e2b4f75986d2ae94fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Sat, 23 Nov 2019 11:40:07 -0600 Subject: [PATCH 01/12] Adding timeout extension method to Future trait --- src/future/future/mod.rs | 13 +++++++++++++ src/future/timeout.rs | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index 5fdaf4b1..fc800cb2 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -15,6 +15,7 @@ cfg_unstable! { use try_race::TryRace; use join::Join; use try_join::TryJoin; + use crate::future::timeout::TimeoutFuture; } extension_trait! { @@ -355,6 +356,18 @@ extension_trait! { { TryJoin::new(self, other) } + + #[doc = r#" + Waits for both the future and a timeout, if the timeout completes before + the future, it returns an TimeoutError. + "#] + #[cfg(any(feature = "unstable", feature = "docs"))] + #[cfg_attr(feature = "docs", doc(cfg(unstable)))] + fn timeout(self, dur: Duration) -> impl Future [TimeoutFuture] + where Self: Sized + { + TimeoutFuture::new(self, dur) + } } impl Future for Box { diff --git a/src/future/timeout.rs b/src/future/timeout.rs index ff87ae4f..c2b6306c 100644 --- a/src/future/timeout.rs +++ b/src/future/timeout.rs @@ -42,7 +42,7 @@ where pin_project! { /// A future that times out after a duration of time. - struct TimeoutFuture { + pub struct TimeoutFuture { #[pin] future: F, #[pin] @@ -50,6 +50,12 @@ pin_project! { } } +impl TimeoutFuture { + pub fn new(future: F, dur: Duration) -> TimeoutFuture { + TimeoutFuture { future: future, delay: Delay::new(dur) } + } +} + impl Future for TimeoutFuture { type Output = Result; 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 02/12] 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 c2b6306c..05aaa450 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 03/12] 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 fc800cb2..569d0320 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 04/12] 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 00000000..e1dfb4e1 --- /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 05/12] 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 e1dfb4e1..e6e4b344 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 06/12] 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 569d0320..efa832a7 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)))] From 84b6d2b27632851469521ddf9554e0fd96714f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Thu, 12 Dec 2019 18:34:02 -0600 Subject: [PATCH 07/12] Removing duplicated tests --- tests/timeout_future.rs | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 tests/timeout_future.rs diff --git a/tests/timeout_future.rs b/tests/timeout_future.rs deleted file mode 100644 index e6e4b344..00000000 --- a/tests/timeout_future.rs +++ /dev/null @@ -1,27 +0,0 @@ -#![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()); - }); -} From 980a1f7834070b1ff7eddc1d8a710840315e98b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Tue, 17 Dec 2019 22:46:25 -0600 Subject: [PATCH 08/12] Correcting docs on function --- src/future/future/mod.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index e302175e..bd972899 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -363,16 +363,18 @@ extension_trait! { # 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()) + # 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()) + # # }); ``` "#] From 1eeb1019e90a71b0ee39a806959d818a0f91a9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Tue, 17 Dec 2019 23:05:06 -0600 Subject: [PATCH 09/12] Fixing example --- src/future/future/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index bd972899..eea0ffb2 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -364,7 +364,10 @@ extension_trait! { # Example ``` # async_std::task::block_on(async { - # + # + use async_std::prelude::*; + use async_std::future; + let fut = future::ready(0); let dur = Duration::from_millis(100); let res = fut.timeout(dur).await; From 97b4901b7528d3aae89d86438461408080c4bcc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Tue, 17 Dec 2019 23:12:09 -0600 Subject: [PATCH 10/12] Fixing tests --- src/future/future/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index eea0ffb2..dea0ade0 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -364,10 +364,13 @@ extension_trait! { # Example ``` # async_std::task::block_on(async { - # + # + use std::time::Duration; + use async_std::prelude::*; use async_std::future; - + use async_std::task; + let fut = future::ready(0); let dur = Duration::from_millis(100); let res = fut.timeout(dur).await; From eedf1d33676c52ba7d2645ba3f55b4ab0b5ee0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Tue, 17 Dec 2019 23:17:02 -0600 Subject: [PATCH 11/12] Fixing docs --- src/future/future/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index dea0ade0..8d956fd7 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -369,7 +369,6 @@ extension_trait! { use async_std::prelude::*; use async_std::future; - use async_std::task; let fut = future::ready(0); let dur = Duration::from_millis(100); From ef021dcb2bac3d1825d8b8162ecbfa41e1740eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Garc=C3=ADa?= Date: Wed, 18 Dec 2019 07:18:57 -0600 Subject: [PATCH 12/12] Changing test condition --- src/future/future/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/future/future/mod.rs b/src/future/future/mod.rs index 8d956fd7..b3efedb0 100644 --- a/src/future/future/mod.rs +++ b/src/future/future/mod.rs @@ -375,10 +375,10 @@ extension_trait! { let res = fut.timeout(dur).await; assert!(res.is_ok()); - let fut = future::ready(0); + let fut = future::pending::<()>(); let dur = Duration::from_millis(100); let res = fut.timeout(dur).await; - assert!(res.is_ok()) + assert!(res.is_err()) # # }); ```