summaryrefslogtreecommitdiffstats
path: root/vendor/tokio/src/io/stdin.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/tokio/src/io/stdin.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tokio/src/io/stdin.rs')
-rw-r--r--vendor/tokio/src/io/stdin.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/vendor/tokio/src/io/stdin.rs b/vendor/tokio/src/io/stdin.rs
new file mode 100644
index 000000000..c9578f17b
--- /dev/null
+++ b/vendor/tokio/src/io/stdin.rs
@@ -0,0 +1,73 @@
+use crate::io::blocking::Blocking;
+use crate::io::{AsyncRead, ReadBuf};
+
+use std::io;
+use std::pin::Pin;
+use std::task::Context;
+use std::task::Poll;
+
+cfg_io_std! {
+ /// 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.
+ ///
+ /// This handle is best used for non-interactive uses, such as when a file
+ /// is piped into the application. For technical reasons, `stdin` is
+ /// implemented by using an ordinary blocking read on a separate thread, and
+ /// it is impossible to cancel that read. This can make shutdown of the
+ /// runtime hang until the user presses enter.
+ ///
+ /// For interactive uses, it is recommended to spawn a thread dedicated to
+ /// user input and use blocking IO directly in that thread.
+ ///
+ /// Created by the [`stdin`] function.
+ ///
+ /// [`stdin`]: fn@stdin
+ /// [`AsyncRead`]: trait@AsyncRead
+ #[derive(Debug)]
+ pub struct Stdin {
+ std: Blocking<std::io::Stdin>,
+ }
+
+ /// Constructs a new handle to the standard input of the current process.
+ ///
+ /// This handle is best used for non-interactive uses, such as when a file
+ /// is piped into the application. For technical reasons, `stdin` is
+ /// implemented by using an ordinary blocking read on a separate thread, and
+ /// it is impossible to cancel that read. This can make shutdown of the
+ /// runtime hang until the user presses enter.
+ ///
+ /// For interactive uses, it is recommended to spawn a thread dedicated to
+ /// user input and use blocking IO directly in that thread.
+ pub fn stdin() -> Stdin {
+ let std = io::stdin();
+ Stdin {
+ std: Blocking::new(std),
+ }
+ }
+}
+
+#[cfg(unix)]
+impl std::os::unix::io::AsRawFd for Stdin {
+ fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
+ std::io::stdin().as_raw_fd()
+ }
+}
+
+#[cfg(windows)]
+impl std::os::windows::io::AsRawHandle for Stdin {
+ fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
+ std::io::stdin().as_raw_handle()
+ }
+}
+
+impl AsyncRead for Stdin {
+ fn poll_read(
+ mut self: Pin<&mut Self>,
+ cx: &mut Context<'_>,
+ buf: &mut ReadBuf<'_>,
+ ) -> Poll<io::Result<()>> {
+ Pin::new(&mut self.std).poll_read(cx, buf)
+ }
+}