summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-fs/examples/std-echo.rs
blob: 83efa66e58390fc09d7d58223ab741e9417c2a09 (plain)
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
//! Echo everything received on STDIN to STDOUT.
#![deny(deprecated, warnings)]

extern crate futures;
extern crate tokio_fs;
extern crate tokio_codec;
extern crate tokio_threadpool;

use tokio_fs::{stdin, stdout, stderr};
use tokio_codec::{FramedRead, FramedWrite, LinesCodec};
use tokio_threadpool::Builder;

use futures::{Future, Stream, Sink};

use std::io;

pub fn main() {
    let pool = Builder::new()
        .pool_size(1)
        .build();

    pool.spawn({
        let input = FramedRead::new(stdin(), LinesCodec::new());

        let output = FramedWrite::new(stdout(), LinesCodec::new())
            .with(|line: String| {
                let mut out = "OUT: ".to_string();
                out.push_str(&line);
                Ok::<_, io::Error>(out)
            });

        let error = FramedWrite::new(stderr(), LinesCodec::new())
            .with(|line: String| {
                let mut out = "ERR: ".to_string();
                out.push_str(&line);
                Ok::<_, io::Error>(out)
            });

        let dst = output.fanout(error);

        input
            .forward(dst)
            .map(|_| ())
            .map_err(|e| panic!("io error = {:?}", e))
    });

    pool.shutdown_on_idle().wait().unwrap();
}