diff options
author | Carl Lerche <me@carllerche.com> | 2019-08-03 21:08:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-03 21:08:29 -0700 |
commit | 337646b97fc4583351ae47e4db13a5e51346afdc (patch) | |
tree | 96884c5abed67293e0e55e104750467a3055e0a6 /tokio/src/util | |
parent | 0bb015588a28a2f7cfd505d611975bfb2123d044 (diff) |
tokio: re-export future/stream utils (#1387)
Diffstat (limited to 'tokio/src/util')
-rw-r--r-- | tokio/src/util/future.rs | 63 | ||||
-rw-r--r-- | tokio/src/util/mod.rs | 14 | ||||
-rw-r--r-- | tokio/src/util/stream.rs | 73 |
3 files changed, 0 insertions, 150 deletions
diff --git a/tokio/src/util/future.rs b/tokio/src/util/future.rs deleted file mode 100644 index a5fb70a0..00000000 --- a/tokio/src/util/future.rs +++ /dev/null @@ -1,63 +0,0 @@ -#[cfg(feature = "timer")] -use tokio_timer::Timeout; - -#[cfg(feature = "timer")] -use std::time::Duration; - -use std::future::Future; - -/// An extension trait for `Future` that provides a variety of convenient -/// combinator functions. -/// -/// Currently, there only is a [`timeout`] function, but this will increase -/// over time. -/// -/// Users are not expected to implement this trait. All types that implement -/// `Future` already implement `FutureExt`. -/// -/// This trait can be imported directly or via the Tokio prelude: `use -/// tokio::prelude::*`. -/// -/// [`timeout`]: #method.timeout -pub trait FutureExt: Future { - /// Creates a new future which allows `self` until `timeout`. - /// - /// This combinator creates a new future which wraps the receiving future - /// with a timeout. The returned future is allowed to execute until it - /// completes or `timeout` has elapsed, whichever happens first. - /// - /// If the future completes before `timeout` then the future will resolve - /// with that item. Otherwise the future will resolve to an error. - /// - /// The future is guaranteed to be polled at least once, even if `timeout` - /// is set to zero. - /// - /// # Examples - /// - /// ``` - /// use tokio::prelude::*; - /// use std::time::Duration; - /// # use futures::future::{self, FutureResult}; - /// - /// # fn long_future() -> FutureResult<(), ()> { - /// # future::ok(()) - /// # } - /// # - /// # fn main() { - /// let future = long_future() - /// .timeout(Duration::from_secs(1)) - /// .map_err(|e| println!("error = {:?}", e)); - /// - /// tokio::run(future); - /// # } - /// ``` - #[cfg(feature = "timer")] - fn timeout(self, timeout: Duration) -> Timeout<Self> - where - Self: Sized, - { - Timeout::new(self, timeout) - } -} - -impl<T: ?Sized> FutureExt for T where T: Future {} diff --git a/tokio/src/util/mod.rs b/tokio/src/util/mod.rs deleted file mode 100644 index 3ebd1fc7..00000000 --- a/tokio/src/util/mod.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Utilities for working with Tokio. -//! -//! This module contains utilities that are useful for working with Tokio. -//! Currently, this only includes [`FutureExt`] and [`StreamExt`], but this -//! may grow over time. -//! -//! [`FutureExt`]: trait.FutureExt.html -//! [`StreamExt`]: trait.StreamExt.html - -mod future; -mod stream; - -pub use self::future::FutureExt; -pub use self::stream::StreamExt; diff --git a/tokio/src/util/stream.rs b/tokio/src/util/stream.rs deleted file mode 100644 index 679a2fc8..00000000 --- a/tokio/src/util/stream.rs +++ /dev/null @@ -1,73 +0,0 @@ -#[cfg(feature = "timer")] -use std::time::Duration; - -#[cfg(feature = "timer")] -use tokio_timer::{throttle::Throttle, Timeout}; - -use futures_core::Stream; - -/// An extension trait for `Stream` that provides a variety of convenient -/// combinator functions. -/// -/// Currently, there are only [`timeout`] and [`throttle`] functions, but -/// this will increase over time. -/// -/// Users are not expected to implement this trait. All types that implement -/// `Stream` already implement `StreamExt`. -/// -/// This trait can be imported directly or via the Tokio prelude: `use -/// tokio::prelude::*`. -/// -/// [`timeout`]: #method.timeout -pub trait StreamExt: Stream { - /// Throttle down the stream by enforcing a fixed delay between items. - /// - /// Errors are also delayed. - #[cfg(feature = "timer")] - fn throttle(self, duration: Duration) -> Throttle<Self> - where - Self: Sized, - { - Throttle::new(self, duration) - } - - /// Creates a new stream which allows `self` until `timeout`. - /// - /// This combinator creates a new stream which wraps the receiving stream - /// with a timeout. For each item, the returned stream is allowed to execute - /// until it completes or `timeout` has elapsed, whichever happens first. - /// - /// If an item completes before `timeout` then the stream will yield - /// with that item. Otherwise the stream will yield to an error. - /// - /// # Examples - /// - /// ``` - /// use tokio::prelude::*; - /// use std::time::Duration; - /// # use futures::future::{self, FutureResult}; - /// - /// # fn long_future() -> FutureResult<(), ()> { - /// # future::ok(()) - /// # } - /// # - /// # fn main() { - /// let stream = long_future() - /// .into_stream() - /// .timeout(Duration::from_secs(1)) - /// .for_each(|i| future::ok(println!("item = {:?}", i))) - /// .map_err(|e| println!("error = {:?}", e)); - /// - /// tokio::run(stream); - /// # } - /// ``` - #[cfg(feature = "timer")] - fn timeout(self, timeout: Duration) -> Timeout<Self> - where - Self: Sized, - { - Timeout::new(self, timeout) - } -} - -impl<T: ?Sized> StreamExt for T where T: Stream {} |