Skip to content

Commit

Permalink
WIP: async start
Browse files Browse the repository at this point in the history
  • Loading branch information
RadicalZephyr committed Jan 31, 2019
1 parent e106086 commit f2f45ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ winapi = { version = "0.3", features = ["consoleapi", "handleapi", "minwindef",
[dev-dependencies]
tempdir = "0.3"
assert_matches = "1.2"
futures = "0.1"
tokio = "0.1"

40 changes: 40 additions & 0 deletions examples/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::thread;

use rustyline::error::ReadlineError;
use rustyline::Editor;

use futures::future::Future;
use futures::stream::Stream;

use tokio::sync::mpsc;

fn main() {
let (mut stdin_tx, stdin_rx) = mpsc::unbounded_channel();
let (mut stdout_tx, stdout_rx) = mpsc::unbounded_channel();

let mut editor = Editor::<()>::new();

let thread = thread::spawn(move || loop {
let line = editor.readline(">");
match &line {
Err(ReadlineError::Interrupted) => break,
Err(ReadlineError::Eof) => break,
_ => (),
}
stdin_tx.try_send(line);
});

let read_all = stdin_rx.for_each(move |line| {
let message = match line {
Ok(line) => format!("Line: {}", line),
Err(ReadlineError::Interrupted) => format!("CTRL-C"),
Err(ReadlineError::Eof) => format!("CTRL-D"),
Err(err) => format!("Error: {:?}", err),
};
stdout_tx.try_send(message);

Ok(())
});

tokio::run(read_all.map_err(|_| ()));
}

0 comments on commit f2f45ed

Please sign in to comment.