mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00

This patch adds a new crate: tokio-fs. This crate provides a wrapper around `std` functionality that can only be performed using blocking operations. This primarily includes filesystem operations, but it also includes standard input, output, and error access as these streams cannot be safely switched to non-blocking mode in a portable way. These wrappers call the `std` functions from within a `blocking` annotation which allows the runtime to compensate for the fact that the thread will potentially remain blocked in a system call.
48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
//! Echo everything received on STDIN to STDOUT.
|
|
|
|
extern crate futures;
|
|
extern crate tokio_fs;
|
|
extern crate tokio_io;
|
|
extern crate tokio_threadpool;
|
|
|
|
use tokio_fs::{stdin, stdout, stderr};
|
|
use tokio_io::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();
|
|
}
|