Merge pull request #33 from async-rs/remove-time-module

Remove time module
tosocketaddrs
Florian Gilcher 5 years ago committed by GitHub
commit 56cd64520d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,13 +8,15 @@ use async_std::io;
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(())
}))
}

@ -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());
/// #
/// # }) }
/// ```

@ -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);
/// #
/// # }) }
/// ```

@ -75,6 +75,5 @@ pub mod prelude;
pub mod stream;
pub mod sync;
pub mod task;
pub mod time;
pub(crate) mod utils;

@ -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 _;

@ -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;
}

@ -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;

@ -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<T>(std::marker::PhantomData<T>);
macro_rules! ret {
($f:tt, $o:ty) => (ImplFuture<$o>);
}
} else {
macro_rules! ret {
($f:tt, $o:ty) => ($f<Self>);
}
}
}
/// 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<TimeoutError> 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<Self::Output, TimeoutError>) {
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<F> {
future: F,
delay: Delay,
}
impl<F> TimeoutFuture<F> {
unsafe_pinned!(future: F);
unsafe_pinned!(delay: Delay);
}
impl<F: Future> Future for TimeoutFuture<F> {
type Output = Result<F::Output, TimeoutError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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<F: Future> Timeout for F {}
Loading…
Cancel
Save