Files
tokio/tokio-fs/src/stdin.rs
T

50 lines
1.4 KiB
Rust
Raw Normal View History

use crate::blocking::Blocking;
use tokio_io::AsyncRead;
use std::io;
2019-07-11 11:05:49 -05:00
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
2018-05-02 11:19:58 -07:00
/// A handle to the standard input stream of a process.
///
/// The handle implements the [`AsyncRead`] trait, but beware that concurrent
/// reads of `Stdin` must be executed with care.
///
/// As an additional caveat, reading from the handle may block the calling
/// future indefinitely, if there is not enough data available. This makes this
/// handle unsuitable for use in any circumstance where immediate reaction to
/// available data is required, e.g. interactive use or when implementing a
/// subprocess driven by requests on the standard input.
///
2018-05-02 11:19:58 -07:00
/// Created by the [`stdin`] function.
///
/// [`stdin`]: fn.stdin.html
/// [`AsyncRead`]: trait.AsyncRead.html
#[derive(Debug)]
pub struct Stdin {
std: Blocking<std::io::Stdin>,
2018-05-02 11:19:58 -07:00
}
/// Constructs a new handle to the standard input of the current process.
///
/// The returned handle allows reading from standard input from the within the
/// Tokio runtime.
pub fn stdin() -> Stdin {
let std = io::stdin();
Stdin {
std: Blocking::new(std),
}
2018-05-02 11:19:58 -07:00
}
impl AsyncRead for Stdin {
2019-07-11 11:05:49 -05:00
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
2019-07-11 11:05:49 -05:00
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.std).poll_read(cx, buf)
2018-05-02 11:19:58 -07:00
}
}