Files
tokio/tokio-fs/examples_old/std-echo.rs
T

45 lines
1.2 KiB
Rust
Raw Normal View History

2018-05-02 11:19:58 -07:00
//! Echo everything received on STDIN to STDOUT.
2018-06-04 22:36:06 -05:00
#![deny(deprecated, warnings)]
2019-07-11 11:05:49 -05:00
#![feature(async_await)]
2018-05-02 11:19:58 -07:00
2018-06-04 22:36:06 -05:00
use tokio_codec::{FramedRead, FramedWrite, LinesCodec};
2019-02-21 11:56:15 -08:00
use tokio_fs::{stderr, stdin, stdout};
2018-05-02 11:19:58 -07:00
use tokio_threadpool::Builder;
2019-07-11 11:05:49 -05:00
use futures_util::sink::SinkExt;
2018-05-02 11:19:58 -07:00
use std::io;
2019-07-11 11:05:49 -05:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(1).build();
2018-05-02 11:19:58 -07:00
pool.spawn({
let input = FramedRead::new(stdin(), LinesCodec::new());
2019-02-21 11:56:15 -08:00
let output = FramedWrite::new(stdout(), LinesCodec::new()).with(|line: String| {
let mut out = "OUT: ".to_string();
out.push_str(&line);
Ok::<_, io::Error>(out)
});
2018-05-02 11:19:58 -07:00
2019-02-21 11:56:15 -08:00
let error = FramedWrite::new(stderr(), LinesCodec::new()).with(|line: String| {
let mut out = "ERR: ".to_string();
out.push_str(&line);
Ok::<_, io::Error>(out)
});
2018-05-02 11:19:58 -07:00
let dst = output.fanout(error);
input
.forward(dst)
.map(|_| ())
.map_err(|e| panic!("io error = {:?}", e))
});
2019-02-21 11:56:15 -08:00
pool.shutdown_on_idle()
.wait()
.map_err(|_| "failed to shutdown the thread pool")?;
Ok(())
2018-05-02 11:19:58 -07:00
}