From e7620dfa9bf6190b17625f2cc55bdf632e1c192a Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Sun, 7 Jan 2024 10:02:39 -0600 Subject: [PATCH 1/2] CI test --- tokio-util/src/time/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tokio-util/src/time/mod.rs b/tokio-util/src/time/mod.rs index 2d340083604..48d88b3a202 100644 --- a/tokio-util/src/time/mod.rs +++ b/tokio-util/src/time/mod.rs @@ -16,6 +16,33 @@ pub mod delay_queue; #[doc(inline)] pub use delay_queue::DelayQueue; +use futures_core::Future; +use tokio::time::Timeout; + +/// A trait which contains a variety of conevenient adapters and utilities for `Future`s. +pub trait FutureExt: Future + Sized { + /// A wrapper around [`tokio::time::timeout`], with the advantage that it is easier to write + /// fluent call chains. + /// + /// # Examples + /// + /// ```rust + /// use tokio::{sync::oneshot, time::Duration}; + /// use tokio_util::time::FutureExt; + /// + /// # async fn dox() { + /// let (tx, rx) = oneshot::channel::<()>(); + /// + /// let res = rx.timeout(Duration::from_millis(10)).await; + /// assert!(res.is_err()); + /// # } + /// ``` + fn timeout(self, timeout: Duration) -> Timeout { + tokio::time::timeout(timeout, self) + } +} + +impl FutureExt for T {} // ===== Internal utils ===== From 73fa17178da9338f52f0dadf5f994362aef1f5ae Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Fri, 12 Jan 2024 07:12:02 -0600 Subject: [PATCH 2/2] Reviewer comments --- tokio-util/src/time/mod.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tokio-util/src/time/mod.rs b/tokio-util/src/time/mod.rs index 48d88b3a202..06d4bd2697e 100644 --- a/tokio-util/src/time/mod.rs +++ b/tokio-util/src/time/mod.rs @@ -8,7 +8,9 @@ //! //! This type must be used from within the context of the `Runtime`. +use futures_core::Future; use std::time::Duration; +use tokio::time::Timeout; mod wheel; @@ -16,11 +18,9 @@ pub mod delay_queue; #[doc(inline)] pub use delay_queue::DelayQueue; -use futures_core::Future; -use tokio::time::Timeout; -/// A trait which contains a variety of conevenient adapters and utilities for `Future`s. -pub trait FutureExt: Future + Sized { +/// A trait which contains a variety of convenient adapters and utilities for `Future`s. +pub trait FutureExt: Future { /// A wrapper around [`tokio::time::timeout`], with the advantage that it is easier to write /// fluent call chains. /// @@ -37,12 +37,15 @@ pub trait FutureExt: Future + Sized { /// assert!(res.is_err()); /// # } /// ``` - fn timeout(self, timeout: Duration) -> Timeout { + fn timeout(self, timeout: Duration) -> Timeout + where + Self: Sized, + { tokio::time::timeout(timeout, self) } } -impl FutureExt for T {} +impl FutureExt for T {} // ===== Internal utils =====