File tree Expand file tree Collapse file tree 2 files changed +12
-20
lines changed Expand file tree Collapse file tree 2 files changed +12
-20
lines changed Original file line number Diff line number Diff line change @@ -9,22 +9,13 @@ use async_std::prelude::*;
99use async_std:: task;
1010
1111fn main ( ) -> io:: Result < ( ) > {
12- task:: block_on ( async {
12+ task:: block_on ( io :: timeout ( Duration :: from_secs ( 5 ) , async {
1313 let stdin = io:: stdin ( ) ;
14- let mut line = String :: new ( ) ;
1514
16- match stdin
17- . read_line ( & mut line)
18- . timeout ( Duration :: from_secs ( 5 ) )
19- . await
20- {
21- Ok ( res) => {
22- res?;
23- print ! ( "Got line: {}" , line) ;
24- }
25- Err ( _) => println ! ( "You have only 5 seconds to enter a line. Try again :)" ) ,
26- }
15+ let mut line = String :: new ( ) ;
16+ stdin. read_line ( & mut line) . await ?;
2717
18+ print ! ( "Got line: {}" , line) ;
2819 Ok ( ( ) )
29- } )
20+ } ) )
3021}
Original file line number Diff line number Diff line change @@ -20,11 +20,12 @@ use crate::task::{Context, Poll};
2020///
2121/// use async_std::io;
2222///
23- /// let stdin = io::stdin();
24- /// let mut line = String::new();
25- ///
26- /// let dur = Duration::from_secs(5);
27- /// let n = io::timeout(dur, stdin.read_line(&mut line)).await?;
23+ /// io::timeout(Duration::from_secs(5), async {
24+ /// let stdin = io::stdin();
25+ /// let mut line = String::new();
26+ /// let n = stdin.read_line(&mut line).await?;
27+ /// })
28+ /// .await?;
2829/// #
2930/// # Ok(()) }) }
3031/// ```
6162 Poll :: Pending => match self . delay ( ) . poll ( cx) {
6263 Poll :: Ready ( _) => Poll :: Ready ( Err ( io:: Error :: new (
6364 io:: ErrorKind :: TimedOut ,
64- "future has timed out" ,
65+ "I/O operation has timed out" ,
6566 ) ) ) ,
6667 Poll :: Pending => Poll :: Pending ,
6768 } ,
You can’t perform that action at this time.
0 commit comments