mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-01 14:06:41 +00:00
commit
487811ed4c
3 changed files with 78 additions and 14 deletions
|
@ -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(())
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ pub use sink::{sink, Sink};
|
|||
pub use stderr::{stderr, Stderr};
|
||||
pub use stdin::{stdin, Stdin};
|
||||
pub use stdout::{stdout, Stdout};
|
||||
pub use timeout::timeout;
|
||||
pub use write::Write;
|
||||
|
||||
mod buf_read;
|
||||
|
@ -46,4 +47,5 @@ mod sink;
|
|||
mod stderr;
|
||||
mod stdin;
|
||||
mod stdout;
|
||||
mod timeout;
|
||||
mod write;
|
||||
|
|
71
src/io/timeout.rs
Normal file
71
src/io/timeout.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use std::pin::Pin;
|
||||
use std::time::Duration;
|
||||
|
||||
use futures_timer::Delay;
|
||||
use pin_utils::unsafe_pinned;
|
||||
|
||||
use crate::future::Future;
|
||||
use crate::io;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
/// Awaits an I/O future 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;
|
||||
///
|
||||
/// 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(()) }) }
|
||||
/// ```
|
||||
pub async fn timeout<F, T>(dur: Duration, f: F) -> io::Result<T>
|
||||
where
|
||||
F: Future<Output = io::Result<T>>,
|
||||
{
|
||||
let f = TimeoutFuture {
|
||||
future: f,
|
||||
delay: Delay::new(dur),
|
||||
};
|
||||
f.await
|
||||
}
|
||||
|
||||
struct TimeoutFuture<F> {
|
||||
future: F,
|
||||
delay: Delay,
|
||||
}
|
||||
|
||||
impl<F> TimeoutFuture<F> {
|
||||
unsafe_pinned!(future: F);
|
||||
unsafe_pinned!(delay: Delay);
|
||||
}
|
||||
|
||||
impl<F, T> Future for TimeoutFuture<F>
|
||||
where
|
||||
F: Future<Output = io::Result<T>>,
|
||||
{
|
||||
type Output = F::Output;
|
||||
|
||||
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(v),
|
||||
Poll::Pending => match self.delay().poll(cx) {
|
||||
Poll::Ready(_) => Poll::Ready(Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"I/O operation has timed out",
|
||||
))),
|
||||
Poll::Pending => Poll::Pending,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue