From 4b45918f50c3662270f7e5fac2364a2cda873994 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Thu, 15 Aug 2019 19:26:10 +0200 Subject: [PATCH] Remove time module --- examples/stdin-timeout.rs | 5 +- src/future/pending.rs | 7 ++- src/future/ready.rs | 4 +- src/lib.rs | 1 - src/prelude.rs | 28 +--------- src/task/sleep.rs | 7 ++- src/time/mod.rs | 33 ------------ src/time/timeout.rs | 107 -------------------------------------- 8 files changed, 14 insertions(+), 178 deletions(-) delete mode 100644 src/time/mod.rs delete mode 100644 src/time/timeout.rs diff --git a/examples/stdin-timeout.rs b/examples/stdin-timeout.rs index 04b860a2..7cc05bf2 100644 --- a/examples/stdin-timeout.rs +++ b/examples/stdin-timeout.rs @@ -5,17 +5,18 @@ use std::time::Duration; use async_std::io; -use async_std::prelude::*; use async_std::task; fn main() -> io::Result<()> { + // This async scope times out after 5 seconds. task::block_on(io::timeout(Duration::from_secs(5), async { let stdin = io::stdin(); + // Read a line from the standard input and display it. let mut line = String::new(); stdin.read_line(&mut line).await?; + dbg!(line); - print!("Got line: {}", line); Ok(()) })) } diff --git a/src/future/pending.rs b/src/future/pending.rs index f45199f2..57a40ea0 100644 --- a/src/future/pending.rs +++ b/src/future/pending.rs @@ -8,10 +8,13 @@ /// use std::time::Duration; /// /// use async_std::future::pending; -/// use async_std::prelude::*; +/// use async_std::io; /// /// let dur = Duration::from_secs(1); -/// assert!(pending::<()>().timeout(dur).await.is_err()); +/// let fut = pending(); +/// +/// let res: io::Result<()> = io::timeout(dur, fut).await; +/// assert!(res.is_err()); /// # /// # }) } /// ``` diff --git a/src/future/ready.rs b/src/future/ready.rs index 7535d504..6438c60e 100644 --- a/src/future/ready.rs +++ b/src/future/ready.rs @@ -10,9 +10,9 @@ /// # #![feature(async_await)] /// # fn main() { async_std::task::block_on(async { /// # -/// use async_std::future::ready; +/// use async_std::future; /// -/// assert_eq!(ready(10).await, 10); +/// assert_eq!(future::ready(10).await, 10); /// # /// # }) } /// ``` diff --git a/src/lib.rs b/src/lib.rs index ad1b0e4f..ff2468a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,5 @@ pub mod prelude; pub mod stream; pub mod sync; pub mod task; -pub mod time; pub(crate) mod utils; diff --git a/src/prelude.rs b/src/prelude.rs index eabd92f8..38956b93 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,30 +1,6 @@ //! The async prelude. //! -//! The prelude re-exports the most commonly used traits in async programming. -//! -//! # Examples -//! -//! Import the prelude to use the [`timeout`] combinator: -//! -//! ```no_run -//! # #![feature(async_await)] -//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { -//! # -//! use std::time::Duration; -//! -//! use async_std::io; -//! use async_std::prelude::*; -//! -//! let stdin = io::stdin(); -//! let mut line = String::new(); -//! let dur = Duration::from_secs(5); -//! -//! stdin.read_line(&mut line).timeout(dur).await??; -//! # -//! # Ok(()) }) } -//! ``` -//! -//! [`timeout`]: ../time/trait.Timeout.html#method.timeout +//! The prelude re-exports the most commonly used traits in this crate. #[doc(no_inline)] pub use crate::future::Future; @@ -40,5 +16,3 @@ pub use crate::io::Write as _; pub use crate::stream::Stream; #[doc(no_inline)] pub use crate::task_local; -#[doc(no_inline)] -pub use crate::time::Timeout as _; diff --git a/src/task/sleep.rs b/src/task/sleep.rs index a0392be6..fa8804eb 100644 --- a/src/task/sleep.rs +++ b/src/task/sleep.rs @@ -1,8 +1,7 @@ use std::time::Duration; -use futures::future; - -use crate::time::Timeout; +use crate::future; +use crate::io; /// Sleeps for the specified amount of time. /// @@ -27,5 +26,5 @@ use crate::time::Timeout; /// # }) } /// ``` pub async fn sleep(dur: Duration) { - let _ = future::pending::<()>().timeout(dur).await; + let _: io::Result<()> = io::timeout(dur, future::pending()).await; } diff --git a/src/time/mod.rs b/src/time/mod.rs deleted file mode 100644 index f8f389cd..00000000 --- a/src/time/mod.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! Timeouts for async operations. -//! -//! This module is an async extension of [`std::time`]. -//! -//! [`std::time`]: https://doc.rust-lang.org/std/time/index.html -//! -//! # Examples -//! -//! Read a line from stdin with a timeout of 5 seconds. -//! -//! ```no_run -//! # #![feature(async_await)] -//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { -//! # -//! use std::time::Duration; -//! -//! use async_std::io; -//! use async_std::prelude::*; -//! -//! let stdin = io::stdin(); -//! let mut line = String::new(); -//! -//! let n = stdin -//! .read_line(&mut line) -//! .timeout(Duration::from_secs(5)) -//! .await??; -//! # -//! # Ok(()) }) } -//! ``` - -pub use timeout::{Timeout, TimeoutError}; - -mod timeout; diff --git a/src/time/timeout.rs b/src/time/timeout.rs deleted file mode 100644 index 9f21bcc4..00000000 --- a/src/time/timeout.rs +++ /dev/null @@ -1,107 +0,0 @@ -use std::error::Error; -use std::fmt; -use std::pin::Pin; -use std::time::Duration; - -use cfg_if::cfg_if; -use futures_timer::Delay; -use pin_utils::unsafe_pinned; - -use crate::future::Future; -use crate::io; -use crate::task::{Context, Poll}; - -cfg_if! { - if #[cfg(feature = "docs")] { - #[doc(hidden)] - pub struct ImplFuture(std::marker::PhantomData); - - macro_rules! ret { - ($f:tt, $o:ty) => (ImplFuture<$o>); - } - } else { - macro_rules! ret { - ($f:tt, $o:ty) => ($f); - } - } -} - -/// An error returned when a future times out. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct TimeoutError; - -impl Error for TimeoutError {} - -impl fmt::Display for TimeoutError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - "future has timed out".fmt(f) - } -} - -impl From for io::Error { - fn from(_: TimeoutError) -> io::Error { - io::Error::new(io::ErrorKind::TimedOut, "future has timed out") - } -} - -/// An extension trait that configures timeouts for futures. -pub trait Timeout: Future + Sized { - /// Awaits a future to completion or times out after a duration of time. - /// - /// # Examples - /// - /// ```no_run - /// # #![feature(async_await)] - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { - /// # - /// use std::time::Duration; - /// - /// use async_std::io; - /// use async_std::prelude::*; - /// - /// let stdin = io::stdin(); - /// let mut line = String::new(); - /// - /// let n = stdin - /// .read_line(&mut line) - /// .timeout(Duration::from_secs(5)) - /// .await??; - /// # - /// # Ok(()) }) } - /// ``` - fn timeout(self, dur: Duration) -> ret!(TimeoutFuture, Result) { - TimeoutFuture { - future: self, - delay: Delay::new(dur), - } - } -} - -/// A future that times out after a duration of time. -#[doc(hidden)] -#[allow(missing_debug_implementations)] -pub struct TimeoutFuture { - future: F, - delay: Delay, -} - -impl TimeoutFuture { - unsafe_pinned!(future: F); - unsafe_pinned!(delay: Delay); -} - -impl Future for TimeoutFuture { - type Output = Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.as_mut().future().poll(cx) { - Poll::Ready(v) => Poll::Ready(Ok(v)), - Poll::Pending => match self.delay().poll(cx) { - Poll::Ready(_) => Poll::Ready(Err(TimeoutError)), - Poll::Pending => Poll::Pending, - }, - } - } -} - -impl Timeout for F {}