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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![deny(missing_docs)]
extern crate futures;
extern crate tokio_core;
extern crate tokio_io;
use futures::{Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
use futures::future::{Either, Loop};
use futures::sink::Send;
use futures::sync::{mpsc, oneshot};
use std::io::{self, Error, ErrorKind, Read, Write};
use std::thread;
use tokio_core::reactor::Core;
type StdioPacket = Result<Vec<u8>, Error>;
struct StdinState<'a> {
first: bool,
buf: [u8; 512],
stdin_lock: io::StdinLock<'a>,
tx_stdin_data: Either<mpsc::Sender<StdioPacket>, Send<mpsc::Sender<StdioPacket>>>,
rx_stdin_stop: oneshot::Receiver<()>,
}
pub fn borrow_stdio<F, T>(f: F) -> Result<T, Error>
where F: FnOnce(StdinStream, StdoutSink) -> Result<T, Error>
{
let (tx_stdin_data, rx_stdin_data) = mpsc::channel(1);
let (tx_stdin_stop, rx_stdin_stop) = oneshot::channel::<()>();
thread::spawn(move || {
let mut core = Core::new().expect("couldn't create core");
let stdin = io::stdin();
let stdin_lock = stdin.lock();
let state = StdinState {
first: true,
buf: [0u8; 512],
stdin_lock: stdin_lock,
tx_stdin_data: Either::A(tx_stdin_data),
rx_stdin_stop: rx_stdin_stop,
};
let read_blocking = futures::future::loop_fn(state, |mut state| {
if state.first {
state.first = false;
return Ok(Loop::Continue(state));
}
if let Ok(Async::NotReady) = state.rx_stdin_stop.poll() {
} else {
return Ok(Loop::Break(()));
}
let tx_stdin_data = match state.tx_stdin_data {
Either::A(tx) => tx,
Either::B(mut send) => match send.poll() {
Ok(Async::NotReady) => {
state.tx_stdin_data = Either::B(send);
return Ok(Loop::Continue(state));
}
Ok(Async::Ready(tx)) => tx,
Err(e) => return Err(e),
},
};
let msg = match state.stdin_lock.read(&mut state.buf[..]) {
Ok(n) => Ok((&state.buf[..n]).to_owned()),
Err(e) => Err(e),
};
state.tx_stdin_data = Either::B(tx_stdin_data.send(msg));
Ok(Loop::Continue(state))
});
if let Err(e) = core.run(read_blocking) {
eprintln!("error reading stdin in background thread: {}", e);
}
});
let wrap_stdin = StdinStream(rx_stdin_data);
let (tx_stdout_cmd, rx_stdout_cmd) = mpsc::channel::<Option<Vec<u8>>>(8);
thread::spawn(move || {
let stdout = io::stdout();
let mut stdout_lock = stdout.lock();
let rx = rx_stdout_cmd.wait();
for item in rx {
match item {
Ok(None) => break,
Ok(Some(data)) => {
let _r = stdout_lock.write(&data);
let _r = stdout_lock.flush();
},
Err(_) => {},
};
}
});
let wrap_stdout = StdoutSink(tx_stdout_cmd.clone());
let r = f(wrap_stdin, wrap_stdout);
let _r = tx_stdin_stop.send(());
let _r = tx_stdout_cmd.send(None);
r
}
#[derive(Debug)]
pub struct StdinStream(mpsc::Receiver<StdioPacket>);
impl Stream for StdinStream {
type Item = Vec<u8>;
type Error = Error;
fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> {
match self.0.poll() {
Err(()) => Err(ErrorKind::Other.into()),
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
Ok(Async::Ready(Some(io_res))) => {
match io_res {
Ok(data) => {
if data.len() == 0 {
Ok(Async::Ready(None))
} else {
Ok(Async::Ready(Some(data)))
}
},
Err(e) => {
if e.kind() == ErrorKind::UnexpectedEof {
Ok(Async::Ready(None))
} else {
Err(e)
}
}
}
}
}
}
}
#[derive(Debug)]
pub struct StdoutSink(mpsc::Sender<Option<Vec<u8>>>);
impl Sink for StdoutSink {
type SinkItem = Vec<u8>;
type SinkError = Error;
fn start_send(&mut self, item: Vec<u8>) -> StartSend<Vec<u8>, Error> {
match self.0.start_send(Some(item)) {
Err(_) => Err(io::ErrorKind::Other.into()),
Ok(AsyncSink::Ready) => Ok(AsyncSink::Ready),
Ok(AsyncSink::NotReady(o)) => Ok(AsyncSink::NotReady(o.unwrap())),
}
}
fn poll_complete(&mut self) -> Poll<(), Error> {
match self.0.poll_complete() {
Err(_) => Err(io::ErrorKind::Other.into()),
Ok(x) => Ok(x),
}
}
}