blob: 768baf107ce373db17c666432278bb086e90e581 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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))
}
}
|