diff --git a/examples/stdin-timeout.rs b/examples/stdin-timeout.rs index dde49e5..04b860a 100644 --- a/examples/stdin-timeout.rs +++ b/examples/stdin-timeout.rs @@ -9,22 +9,13 @@ use async_std::prelude::*; use async_std::task; fn main() -> io::Result<()> { - task::block_on(async { + task::block_on(io::timeout(Duration::from_secs(5), async { let stdin = io::stdin(); + let mut line = String::new(); + stdin.read_line(&mut line).await?; - match stdin - .read_line(&mut line) - .timeout(Duration::from_secs(5)) - .await - { - Ok(res) => { - res?; - print!("Got line: {}", line); - } - Err(_) => println!("You have only 5 seconds to enter a line. Try again :)"), - } - + print!("Got line: {}", line); Ok(()) - }) + })) } diff --git a/src/io/timeout.rs b/src/io/timeout.rs index 5902989..b35a803 100644 --- a/src/io/timeout.rs +++ b/src/io/timeout.rs @@ -20,11 +20,12 @@ use crate::task::{Context, Poll}; /// /// use async_std::io; /// -/// let stdin = io::stdin(); -/// let mut line = String::new(); -/// -/// let dur = Duration::from_secs(5); -/// let n = io::timeout(dur, stdin.read_line(&mut line)).await?; +/// io::timeout(Duration::from_secs(5), async { +/// let stdin = io::stdin(); +/// let mut line = String::new(); +/// let n = stdin.read_line(&mut line).await?; +/// }) +/// .await?; /// # /// # Ok(()) }) } /// ``` @@ -61,7 +62,7 @@ where Poll::Pending => match self.delay().poll(cx) { Poll::Ready(_) => Poll::Ready(Err(io::Error::new( io::ErrorKind::TimedOut, - "future has timed out", + "I/O operation has timed out", ))), Poll::Pending => Poll::Pending, },