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
//! Spawn a new thread that writes to stdout using a Tokio encoder.
//!
//! This is not production-ready:
//! - Items that are flushed to the sink are not guaranteed to be written to stdout (eek!)
//! - Errors are not bubbled up correctly
//! - The user should be able to limit the size of the `BytesMut`
//! - A more thoughful treatment of performance tradeoffs would be nice
//!
//! ```rust
//! extern crate futures;
//! extern crate tokio_fmt_encoder;
//! extern crate tokio_io;
//! extern crate tokio_stdout;
//!
//! use futures::{Future, Stream};
//! use futures::sync::mpsc::SendError;
//! use futures::stream::iter_ok;
//! use tokio_fmt_encoder::DebugEncoder;
//! use tokio_stdout::spawn_encoder_sink_bounded;
//!
//! fn main() {
//!     let encoder: DebugEncoder<usize> = Default::default();
//!
//!     iter_ok::<_, SendError<_>>((1..10).into_iter())
//!         .forward(spawn_encoder_sink_bounded(encoder, 1))
//!         .wait()
//!         .unwrap();
//! }
//! ```
#![deny(warnings)]
#![allow(deprecated)]
extern crate bytes;
extern crate futures;
extern crate tokio_io;

use bytes::BytesMut;
use futures::Stream;
use futures::sync::mpsc::{Sender, UnboundedSender, channel, unbounded};
use std::fmt::Debug;
use std::io::{self, Write};
use std::thread;
use tokio_io::codec::Encoder;


/// Spawn a new thread that encodes each item and writes it to stdout. The channel to send items to
/// the spawned thread is bounded in size.
pub fn spawn_encoder_sink_bounded<I, E, N>(mut encoder: N, buffer_size: usize) -> Sender<I>
where
    I: Send + 'static,
    E: std::convert::From<std::io::Error> + Debug + 'static,
    N: Encoder<Item = I, Error = E> + Send + 'static,
{
    let (channel_sink, channel_stream) = channel::<I>(buffer_size);
    let mut buffer: BytesMut = Default::default();
    thread::spawn(move || {
        let mut stdout = io::stdout();

        for s in channel_stream.wait() {
            encoder.encode(s.unwrap(), &mut buffer).unwrap();
            stdout.write_all(&buffer.take()).unwrap();
        }
    });
    channel_sink
}

/// Spawn a new thread that encodes each item and writes it to stdout. The channel to send items to
/// the spawned thread is unbounded in size.
pub fn spawn_encoder_sink_unbounded<I, E, N>(mut encoder: N) -> UnboundedSender<I>
where
    I: Send + 'static,
    E: std::convert::From<std::io::Error> + Debug + 'static,
    N: Encoder<Item = I, Error = E> + Send + 'static,
{
    let (channel_sink, channel_stream) = unbounded::<I>();
    let mut buffer: BytesMut = Default::default();
    thread::spawn(move || {
        let mut stdout = io::stdout();

        for s in channel_stream.wait() {
            encoder.encode(s.unwrap(), &mut buffer).unwrap();
            stdout.write_all(&buffer.take()).unwrap();
        }
    });
    channel_sink
}