Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(await_macro, async_await, futures_api)]
- #[macro_use]
- extern crate tokio;
- extern crate futures;
- extern crate hyper;
- extern crate hyper_tls;
- extern crate tokio_stdin_stdout;
- use tokio::prelude::{AsyncWriteExt, Stream as TokioStream, Future, FutureExt, AsyncReadExt};
- use std::str;
- use futures::channel::mpsc::unbounded;
- use futures::StreamExt;
- use futures::channel::mpsc::{UnboundedSender, UnboundedReceiver};
- async fn write_stdout(mut recv: UnboundedReceiver<Uri>) {
- let mut stdout = tokio_stdin_stdout::stdout(10);
- while let Some(url) = await!(recv.next()) {
- await!(stdout.write_async(b"got:\n"));
- let parts = url.into_parts();
- if let Some(scheme) = parts.scheme {
- await!(stdout.write_async(scheme.as_ref().as_bytes()));
- }
- if let Some(authority) = parts.authority {
- await!(stdout.write_async(authority.as_ref().as_bytes()));
- }
- if let Some(path_and_query) = parts.path_and_query {
- await!(stdout.write_async(path_and_query.path().as_bytes()));
- }
- await!(stdout.write_async(b"\n"));
- }
- await!(stdout.flush_async());
- }
- // read stdin, parse to a uri, and send it to the receiver
- async fn sending(mut sender: UnboundedSender<Uri>) {
- let mut stdin = tokio_stdin_stdout::stdin(10);
- let mut buff: Vec<u8> = vec![0; 2014];
- let bytes_read = await!(stdin.read_async(&mut buff)).unwrap();
- let s = str::from_utf8(&buff[..bytes_read - 2]).unwrap();
- // Why does this allways error that the receiver is gone?
- sender.start_send(s.parse().unwrap()).map_err(|e| eprintln!("err {}", e));
- }
- fn main() {
- tokio::run_async(async move {
- let (sender, recv) = unbounded::<Uri>();
- tokio::spawn_async(sending(sender));
- tokio::spawn_async(write_stdout(recv));
- });
- }
Add Comment
Please, Sign In to add comment