Skip to content

Commit

Permalink
First async solution
Browse files Browse the repository at this point in the history
  • Loading branch information
zzeroo committed Sep 29, 2020
1 parent ba35c0b commit dd5006f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures = "0.1"
35 changes: 34 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
extern crate futures; // [dependencies] futures = "0.1"

use std::io::BufRead;
use std::thread;

use futures::sync::mpsc::{Sender, channel};
use futures::{Future, Stream, Sink};

fn main() {
println!("Hello, world!");
let mut worker = spawn_worker();

let stdin = ::std::io::stdin();
for line in stdin.lock().lines() {
let line = line.unwrap();
worker = worker.send(Msg::Echo(line)).wait().unwrap();
}

println!("Bye!");
}

enum Msg {
Echo(String),
}

fn spawn_worker() -> Sender<Msg> {
let (tx, rx) = channel(1);
thread::spawn(move || {
rx.for_each(|msg| {
match msg {
Msg::Echo(msg) => println!("{} <3", msg),
}
Ok(())
}).wait().unwrap()
});
tx
}

0 comments on commit dd5006f

Please sign in to comment.