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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#![deny(missing_docs)]

//! A bridge between [std::io::std{in,out}][1] and Future's AsyncRead/AsyncWrite worlds.
//! [1]:https://doc.rust-lang.org/std/io/fn.stdin.html
//!
//! Example:
//!
//! ```rust,no_run
//! extern crate tokio_core;
//! extern crate tokio_io;
//! extern crate tokio_stdin_stdout;
//!
//! let mut core = tokio_core::reactor::Core::new().unwrap();
//!
//! let stdin = tokio_stdin_stdout::stdin(0);
//! let stdout = tokio_stdin_stdout::stdout(0);
//!
//! core.run(tokio_io::io::copy(stdin, stdout)).unwrap();
//! ```
//!
//! It works by starting separate threads, which do actual synchronous I/O and communicating to
//! the asynchronous world using [future::sync::mpsc](http://alexcrichton.com/futures-rs/futures/sync/mpsc/index.html).
//!
//! For Unix (Linux, OS X) better use [tokio-file-unix](https://crates.io/crates/tokio-file-unix).
//!
//! Concerns:
//!
//! * stdin/stdout are not expected to be ever normally used after using functions from this crate
//! * Allocation-heavy.
//! * All errors collapsed to ErrorKind::Other (for stdout) or ErrorKind::BrokenPipe (for stdin)
//! * Failure to write to stdout is only seen after attempting to send there about 3 more buffers.

extern crate futures;
extern crate tokio_io;

const BUFSIZ: usize = 8192;

use futures::{Async, AsyncSink, Future, Poll, Sink, Stream};
use std::cell::RefCell;
use std::io::{Error, ErrorKind, Read, Result, Write};
use std::rc::Rc;
use std::sync::{Arc, LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
use std::thread::JoinHandle;
use tokio_io::{AsyncRead, AsyncWrite};

type BBR = futures::sync::mpsc::Receiver<Box<[u8]>>;
type BBS = futures::sync::mpsc::Sender<Box<[u8]>>;

/// Asynchronous stdin
pub struct ThreadedStdin {
    debt: Option<Box<[u8]>>,
    rcv: BBR,
}

impl ThreadedStdin {
    /// Wrap into `Arc<Mutex>` to make it clonable and sendable
    pub fn make_sendable(self) -> SendableStdin {
        SendableStdin::new(self)
    }
    /// Wrap into `Rc<RefCell>` to make it clonable
    pub fn make_clonable(self) -> ClonableStdin {
        ClonableStdin::new(self)
    }
}

/// Constructor for the `ThreadedStdin`
pub fn stdin(queue_size: usize) -> ThreadedStdin {
    let (snd_, rcv): (BBS, BBR) = futures::sync::mpsc::channel(queue_size);
    std::thread::spawn(move || {
        let mut snd = snd_;
        let sin = ::std::io::stdin();
        let mut sin_lock = sin.lock();
        let mut buf = vec![0; BUFSIZ];
        while let Ok(ret) = sin_lock.read(&mut buf[..]) {
            let content = buf[0..ret].to_vec().into_boxed_slice();
            snd = match snd.send(content).wait() {
                Ok(x) => x,
                Err(_) => break,
            }
        }
    });
    ThreadedStdin { debt: None, rcv }
}

impl AsyncRead for ThreadedStdin {}
impl Read for ThreadedStdin {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let mut handle_the_buffer = |incoming_buf: Box<[u8]>| {
            let l = buf.len();
            let dl = incoming_buf.len();
            if l >= dl {
                buf[0..dl].copy_from_slice(&incoming_buf);
                (None, Ok(dl))
            } else {
                buf[0..l].copy_from_slice(&incoming_buf[0..l]);
                let newdebt = Some(incoming_buf[l..].to_vec().into_boxed_slice());
                (newdebt, Ok(l))
            }
        };

        let (new_debt, ret) = if let Some(debt) = self.debt.take() {
            handle_the_buffer(debt)
        } else {
            match self.rcv.poll() {
                Ok(Async::Ready(Some(newbuf))) => handle_the_buffer(newbuf),
                Ok(Async::Ready(None)) => (None, Err(ErrorKind::BrokenPipe.into())),
                Ok(Async::NotReady) => (None, Err(ErrorKind::WouldBlock.into())),
                Err(_) => (None, Err(ErrorKind::Other.into())),
            }
        };
        self.debt = new_debt;
        ret
    }
}

/// Asynchronous stdout
pub struct ThreadedStdout {
    snd: BBS,
    jh: Option<JoinHandle<()>>,
}

impl ThreadedStdout {
    /// Wrap into `Arc<Mutex>` to make it clonable and sendable
    pub fn make_sendable(self) -> SendableStdout {
        SendableStdout::new(self)
    }
    /// Wrap into `Rc<RefCell>` to make it clonable
    pub fn make_clonable(self) -> ClonableStdout {
        ClonableStdout::new(self)
    }
}
/// Constructor for the `ThreadedStdout`
pub fn stdout(queue_size: usize) -> ThreadedStdout {
    let (snd, rcv): (BBS, BBR) = futures::sync::mpsc::channel(queue_size);
    let jh = std::thread::spawn(move || {
        let sout = ::std::io::stdout();
        let mut sout_lock = sout.lock();
        for b in rcv.wait() {
            if let Ok(b) = b {
                if b.len() == 0 {
                    break;
                }
                if sout_lock.write_all(&b).is_err() {
                    break;
                }
                if sout_lock.flush().is_err() {
                    break;
                }
            } else {
                break;
            }
        }
        let _ = sout_lock.write(&[]);
    });
    ThreadedStdout { snd, jh: Some(jh) }
}

impl AsyncWrite for ThreadedStdout {
    fn shutdown(&mut self) -> Poll<(), Error> {
        // Signal the thread to exit.
        match self.snd.start_send(vec![].into_boxed_slice()) {
            Ok(AsyncSink::Ready) => (),
            Ok(AsyncSink::NotReady(_)) => return Ok(Async::NotReady),
            Err(_) => {
                return Ok(Async::Ready(()));
            }
        };
        match self.snd.poll_complete() {
            Ok(Async::Ready(_)) => (),
            Ok(Async::NotReady) => return Ok(Async::NotReady),
            Err(e) => {
                return Ok(Async::Ready(()));
            }
        };
        if self.snd.close().is_err() {
            return Ok(Async::Ready(()));
        };
        if let Some(jh) = self.jh.take() {
            if jh.join().is_err() {
                return Err(ErrorKind::Other.into());
            };
        }
        Ok(Async::Ready(()))
    }
}
impl Write for ThreadedStdout {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }

        match self.snd.start_send(buf.to_vec().into_boxed_slice()) {
            Ok(AsyncSink::Ready) => (),
            Ok(AsyncSink::NotReady(_)) => return Err(ErrorKind::WouldBlock.into()),
            Err(_) => return Err(ErrorKind::Other.into()),
        }

        Ok(buf.len())
    }
    fn flush(&mut self) -> Result<()> {
        match self.snd.poll_complete() {
            Ok(Async::Ready(_)) => Ok(()),
            Ok(Async::NotReady) => Err(ErrorKind::WouldBlock.into()),
            Err(_) => Err(ErrorKind::Other.into()),
        }
    }
}

// XXX code duplication:

/// Asynchronous stderr
pub type ThreadedStderr = ThreadedStdout;
/// Constructor for the `ThreadedStderr`
pub fn stderr(queue_size: usize) -> ThreadedStderr {
    let (snd, rcv): (BBS, BBR) = futures::sync::mpsc::channel(queue_size);
    let jh = std::thread::spawn(move || {
        let sout = ::std::io::stderr();
        let mut sout_lock = sout.lock();
        for b in rcv.wait() {
            if let Ok(b) = b {
                if b.len() == 0 {
                    break;
                }
                if sout_lock.write_all(&b).is_err() {
                    break;
                }
                if sout_lock.flush().is_err() {
                    break;
                }
            } else {
                break;
            }
        }
        let _ = sout_lock.write(&[]);
    });
    ThreadedStdout { snd, jh: Some(jh) }
}

/// A sendable and clonable ThreadedStdout wrapper based on `Arc<Mutex<ThreadedStdout>>`
///
/// Note that a mutex is being locked every time a write is performed,
/// so performance may be a problem, unless you use the `lock` method.
///
/// Also note that if data is outputted using multiple `Write::write` calls from multiple tasks, the order of chunks is not specified.
#[derive(Clone)]
pub struct SendableStdout(Arc<Mutex<ThreadedStdout>>);

/// Result of `SendableStdout::lock` or `SendableStdout::try_lock`
pub struct SendableStdoutGuard<'a>(MutexGuard<'a, ThreadedStdout>);

impl SendableStdout {
    /// wrap ThreadedStdout or ThreadedStderr in a sendable/clonable wrapper
    pub fn new(so: ThreadedStdout) -> SendableStdout {
        SendableStdout(Arc::new(Mutex::new(so)))
    }

    /// Acquire more permanent mutex guard on stdout, like with `std::io::Stdout::lock`
    /// The returned guard also implements AsyncWrite
    pub fn lock(&self) -> LockResult<SendableStdoutGuard> {
        match self.0.lock() {
            Ok(x) => Ok(SendableStdoutGuard(x)),
            Err(e) => Err(PoisonError::new(SendableStdoutGuard(e.into_inner()))),
        }
    }
    /// Acquire more permanent mutex guard on stdout
    /// The returned guard also implements AsyncWrite
    pub fn try_lock(&self) -> TryLockResult<SendableStdoutGuard> {
        match self.0.try_lock() {
            Ok(x) => Ok(SendableStdoutGuard(x)),
            Err(TryLockError::Poisoned(e)) => Err(TryLockError::Poisoned(PoisonError::new(
                SendableStdoutGuard(e.into_inner()),
            ))),
            Err(TryLockError::WouldBlock) => Err(TryLockError::WouldBlock),
        }
    }
}

impl Write for SendableStdout {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        match self.0.lock() {
            Ok(mut l) => l.write(buf),
            Err(e) => Err(Error::new(ErrorKind::Other, format!("{}", e))),
        }
    }
    fn flush(&mut self) -> Result<()> {
        match self.0.lock() {
            Ok(mut l) => l.flush(),
            Err(e) => Err(Error::new(ErrorKind::Other, format!("{}", e))),
        }
    }
}
impl AsyncWrite for SendableStdout {
    fn shutdown(&mut self) -> Poll<(), Error> {
        match self.0.lock() {
            Ok(mut l) => l.shutdown(),
            Err(e) => Err(Error::new(ErrorKind::Other, format!("{}", e))),
        }
    }
}
impl<'a> Write for SendableStdoutGuard<'a> {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.0.write(buf)
    }
    fn flush(&mut self) -> Result<()> {
        self.0.flush()
    }
}
impl<'a> AsyncWrite for SendableStdoutGuard<'a> {
    fn shutdown(&mut self) -> Poll<(), Error> {
        self.0.shutdown()
    }
}

/// A clonable `ThreadedStdout` wrapper based on `Rc<RefCell<ThreadedStdout>>`
/// If you need `Send`, use SendableStdout
#[derive(Clone)]
pub struct ClonableStdout(Rc<RefCell<ThreadedStdout>>);
impl ClonableStdout {
    /// wrap ThreadedStdout or ThreadedStderr in a sendable/clonable wrapper
    pub fn new(so: ThreadedStdout) -> ClonableStdout {
        ClonableStdout(Rc::new(RefCell::new(so)))
    }
}

impl Write for ClonableStdout {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.0.borrow_mut().write(buf)
    }
    fn flush(&mut self) -> Result<()> {
        self.0.borrow_mut().flush()
    }
}
impl AsyncWrite for ClonableStdout {
    fn shutdown(&mut self) -> Poll<(), Error> {
        self.0.borrow_mut().shutdown()
    }
}

/// Alias for SendableStdout to avoid confusion of SendableStdout being used for stderr.
///
/// Note that a mutex is being locked every time a read is performed,
/// so performance may be a problem.
///
/// Also note that if data is outputted using multiple `Write::write` calls from multiple tasks, the order of chunks is not specified.
pub type SendableStderr = SendableStdout;
/// Result of `SendableStderr::lock` or `SendableStderr::try_lock`
pub type SendableStderrGuard<'a> = SendableStdoutGuard<'a>;
/// Alias for ClonableStdout to avoid confusion of ClonableStdout being used for stderr.
pub type ClonableStderr = ClonableStdout;

/// A clonable `ThreadedStdout` wrapper based on `Rc<RefCell<ThreadedStdout>>`
/// If you need `Send`, use SendableStdout
///
/// Note that data being read is not duplicated across cloned readers used from multiple tasks.
/// Be careful about corruption.
#[derive(Clone)]
pub struct ClonableStdin(Rc<RefCell<ThreadedStdin>>);
impl ClonableStdin {
    /// wrap ThreadedStdout or ThreadedStderr in a sendable/clonable wrapper
    pub fn new(so: ThreadedStdin) -> ClonableStdin {
        ClonableStdin(Rc::new(RefCell::new(so)))
    }
}

impl Read for ClonableStdin {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.0.borrow_mut().read(buf)
    }
}
impl AsyncRead for ClonableStdin {}
/// A sendable and clonable ThreadedStdin wrapper based on `Arc<Mutex<ThreadedStdin>>`
///
/// Note that data being read is not duplicated across cloned readers used from multiple tasks.
/// Be careful about corruption.
#[derive(Clone)]
pub struct SendableStdin(Arc<Mutex<ThreadedStdin>>);

/// Result of `SendableStdin::lock` or `SendableStdin::try_lock`
pub struct SendableStdinGuard<'a>(MutexGuard<'a, ThreadedStdin>);

impl SendableStdin {
    /// wrap ThreadedStdin in a sendable/clonable wrapper
    pub fn new(si: ThreadedStdin) -> SendableStdin {
        SendableStdin(Arc::new(Mutex::new(si)))
    }

    /// Acquire more permanent mutex guard on stdout, like with `std::io::Stdout::lock`
    /// The returned guard also implements AsyncWrite
    pub fn lock(&self) -> LockResult<SendableStdinGuard> {
        match self.0.lock() {
            Ok(x) => Ok(SendableStdinGuard(x)),
            Err(e) => Err(PoisonError::new(SendableStdinGuard(e.into_inner()))),
        }
    }
    /// Acquire more permanent mutex guard on stdout
    /// The returned guard also implements AsyncWrite
    pub fn try_lock(&self) -> TryLockResult<SendableStdinGuard> {
        match self.0.try_lock() {
            Ok(x) => Ok(SendableStdinGuard(x)),
            Err(TryLockError::Poisoned(e)) => Err(TryLockError::Poisoned(PoisonError::new(
                SendableStdinGuard(e.into_inner()),
            ))),
            Err(TryLockError::WouldBlock) => Err(TryLockError::WouldBlock),
        }
    }
}

impl Read for SendableStdin {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        match self.0.lock() {
            Ok(mut l) => l.read(buf),
            Err(e) => Err(Error::new(ErrorKind::Other, format!("{}", e))),
        }
    }
}
impl AsyncRead for SendableStdin {}

impl<'a> Read for SendableStdinGuard<'a> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.0.read(buf)
    }
}
impl<'a> AsyncRead for SendableStdinGuard<'a> {}