aboutsummaryrefslogtreecommitdiffstats
path: root/src/async_stdin.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-28 13:28:06 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-28 13:28:06 -0400
commit1a2bc078ef06b3a28d5c8ca5f2f495d63c3afbd8 (patch)
treeedaaa8ac2f545f3694b386f2baefc8312f0dfb52 /src/async_stdin.rs
parent607e9a1f1cbaa3f08d2d4c109821c6e987fa5a7e (diff)
downloadnbsh-old-1a2bc078ef06b3a28d5c8ca5f2f495d63c3afbd8.tar.gz
nbsh-old-1a2bc078ef06b3a28d5c8ca5f2f495d63c3afbd8.zip
move to tokio-pty-process-stream
Diffstat (limited to 'src/async_stdin.rs')
-rw-r--r--src/async_stdin.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/async_stdin.rs b/src/async_stdin.rs
new file mode 100644
index 0000000..e3b0ead
--- /dev/null
+++ b/src/async_stdin.rs
@@ -0,0 +1,88 @@
+struct EventedStdin;
+
+const STDIN: i32 = 0;
+
+impl std::io::Read for EventedStdin {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ let stdin = std::io::stdin();
+ let mut stdin = stdin.lock();
+ stdin.read(buf)
+ }
+}
+
+impl mio::Evented for EventedStdin {
+ fn register(
+ &self,
+ poll: &mio::Poll,
+ token: mio::Token,
+ interest: mio::Ready,
+ opts: mio::PollOpt,
+ ) -> std::io::Result<()> {
+ let fd = STDIN as std::os::unix::io::RawFd;
+ let eventedfd = mio::unix::EventedFd(&fd);
+ eventedfd.register(poll, token, interest, opts)
+ }
+
+ fn reregister(
+ &self,
+ poll: &mio::Poll,
+ token: mio::Token,
+ interest: mio::Ready,
+ opts: mio::PollOpt,
+ ) -> std::io::Result<()> {
+ let fd = STDIN as std::os::unix::io::RawFd;
+ let eventedfd = mio::unix::EventedFd(&fd);
+ eventedfd.reregister(poll, token, interest, opts)
+ }
+
+ fn deregister(&self, poll: &mio::Poll) -> std::io::Result<()> {
+ let fd = STDIN as std::os::unix::io::RawFd;
+ let eventedfd = mio::unix::EventedFd(&fd);
+ eventedfd.deregister(poll)
+ }
+}
+
+pub struct Stdin {
+ input: tokio::reactor::PollEvented2<EventedStdin>,
+}
+
+impl Stdin {
+ pub fn new() -> Self {
+ Self {
+ input: tokio::reactor::PollEvented2::new(EventedStdin),
+ }
+ }
+}
+
+impl std::io::Read for Stdin {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ self.input.read(buf)
+ }
+}
+
+impl tokio::io::AsyncRead for Stdin {
+ fn poll_read(
+ &mut self,
+ buf: &mut [u8],
+ ) -> std::result::Result<futures::Async<usize>, tokio::io::Error> {
+ // XXX this is why i had to do the EventedFd thing - poll_read on its
+ // own will block reading from stdin, so i need a way to explicitly
+ // check readiness before doing the read
+ let ready = mio::Ready::readable();
+ match self.input.poll_read_ready(ready)? {
+ futures::Async::Ready(_) => {
+ let res = self.input.poll_read(buf);
+
+ // XXX i'm pretty sure this is wrong (if the single poll_read
+ // call didn't return all waiting data, clearing read ready
+ // state means that we won't get the rest until some more data
+ // beyond that appears), but i don't know that there's a way
+ // to do it correctly given that poll_read blocks
+ self.input.clear_read_ready(ready)?;
+
+ res
+ }
+ futures::Async::NotReady => Ok(futures::Async::NotReady),
+ }
+ }
+}