aboutsummaryrefslogtreecommitdiffstats
path: root/examples/input
diff options
context:
space:
mode:
Diffstat (limited to 'examples/input')
-rw-r--r--examples/input/buf.rs43
-rw-r--r--examples/input/evented_stdin.rs88
-rw-r--r--examples/input/mod.rs2
3 files changed, 133 insertions, 0 deletions
diff --git a/examples/input/buf.rs b/examples/input/buf.rs
new file mode 100644
index 0000000..768baf1
--- /dev/null
+++ b/examples/input/buf.rs
@@ -0,0 +1,43 @@
+use std::io::Read as _;
+
+pub struct Stdin {
+ buf: Vec<u8>,
+ task: futures::task::Task,
+}
+
+#[allow(dead_code)]
+impl Stdin {
+ pub fn new() -> Self {
+ Self {
+ buf: vec![],
+ task: futures::task::current(),
+ }
+ }
+
+ pub fn send(&mut self, buf: &[u8]) {
+ self.buf.extend(buf.iter());
+ self.task.notify();
+ }
+}
+
+impl std::io::Read for Stdin {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ let len = self.buf.len().min(buf.len());
+ buf[..len].clone_from_slice(&self.buf[..len]);
+ self.buf = self.buf.iter().copied().skip(len).collect();
+ Ok(len)
+ }
+}
+
+impl tokio::io::AsyncRead for Stdin {
+ fn poll_read(
+ &mut self,
+ buf: &mut [u8],
+ ) -> std::result::Result<futures::Async<usize>, tokio::io::Error> {
+ if self.buf.is_empty() {
+ return Ok(futures::Async::NotReady);
+ }
+ let n = self.read(buf)?;
+ Ok(futures::Async::Ready(n))
+ }
+}
diff --git a/examples/input/evented_stdin.rs b/examples/input/evented_stdin.rs
new file mode 100644
index 0000000..107c394
--- /dev/null
+++ b/examples/input/evented_stdin.rs
@@ -0,0 +1,88 @@
+// this is a hack around the fact that tokio::io::stdin() is actually
+// blocking, which makes it useless for interactive programs. this isn't great
+// (or particularly correct) but it mostly works.
+
+use std::io::Read as _;
+
+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>,
+}
+
+#[allow(dead_code)]
+impl Stdin {
+ pub fn new() -> Self {
+ Default::default()
+ }
+}
+
+impl Default for Stdin {
+ fn default() -> 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> {
+ let ready = mio::Ready::readable();
+ futures::try_ready!(self.input.poll_read_ready(ready));
+
+ let res = self.read(buf)?;
+ self.input.clear_read_ready(ready)?;
+ Ok(futures::Async::Ready(res))
+ }
+}
diff --git a/examples/input/mod.rs b/examples/input/mod.rs
new file mode 100644
index 0000000..7288cbc
--- /dev/null
+++ b/examples/input/mod.rs
@@ -0,0 +1,2 @@
+pub mod buf;
+pub mod evented_stdin;