tokio_fs/stdin.rs
1use tokio_io::AsyncRead;
2
3use std::io::{self, Read, Stdin as StdStdin};
4
5/// A handle to the standard input stream of a process.
6///
7/// The handle implements the [`AsyncRead`] trait, but beware that concurrent
8/// reads of `Stdin` must be executed with care.
9///
10/// As an additional caveat, reading from the handle may block the calling
11/// future indefinitely, if there is not enough data available. This makes this
12/// handle unsuitable for use in any circumstance where immediate reaction to
13/// available data is required, e.g. interactive use or when implementing a
14/// subprocess driven by requests on the standard input.
15///
16/// Created by the [`stdin`] function.
17///
18/// [`stdin`]: fn.stdin.html
19/// [`AsyncRead`]: trait.AsyncRead.html
20#[derive(Debug)]
21pub struct Stdin {
22 std: StdStdin,
23}
24
25/// Constructs a new handle to the standard input of the current process.
26///
27/// The returned handle allows reading from standard input from the within the
28/// Tokio runtime.
29pub fn stdin() -> Stdin {
30 let std = io::stdin();
31 Stdin { std }
32}
33
34impl Read for Stdin {
35 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
36 ::would_block(|| self.std.read(buf))
37 }
38}
39
40impl AsyncRead for Stdin {
41 unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
42 false
43 }
44}