broker_tokio/io/
stdin.rs

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