Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #![feature(await_macro, async_await, futures_api)]
  2.  
  3. #[macro_use]
  4. extern crate tokio;
  5. extern crate futures;
  6. extern crate hyper;
  7. extern crate hyper_tls;
  8. extern crate tokio_stdin_stdout;
  9.  
  10. use tokio::prelude::{AsyncWriteExt, Stream as TokioStream, Future, FutureExt, AsyncReadExt};
  11.  
  12. use std::str;
  13.  
  14. use futures::channel::mpsc::unbounded;
  15. use futures::StreamExt;
  16.  
  17. use futures::channel::mpsc::{UnboundedSender, UnboundedReceiver};
  18.  
  19. async fn write_stdout(mut recv: UnboundedReceiver<Uri>) {
  20. let mut stdout = tokio_stdin_stdout::stdout(10);
  21.  
  22. while let Some(url) = await!(recv.next()) {
  23. await!(stdout.write_async(b"got:\n"));
  24. let parts = url.into_parts();
  25.  
  26. if let Some(scheme) = parts.scheme {
  27. await!(stdout.write_async(scheme.as_ref().as_bytes()));
  28. }
  29.  
  30. if let Some(authority) = parts.authority {
  31. await!(stdout.write_async(authority.as_ref().as_bytes()));
  32. }
  33.  
  34. if let Some(path_and_query) = parts.path_and_query {
  35. await!(stdout.write_async(path_and_query.path().as_bytes()));
  36. }
  37.  
  38. await!(stdout.write_async(b"\n"));
  39. }
  40.  
  41. await!(stdout.flush_async());
  42. }
  43.  
  44. // read stdin, parse to a uri, and send it to the receiver
  45. async fn sending(mut sender: UnboundedSender<Uri>) {
  46. let mut stdin = tokio_stdin_stdout::stdin(10);
  47.  
  48. let mut buff: Vec<u8> = vec![0; 2014];
  49. let bytes_read = await!(stdin.read_async(&mut buff)).unwrap();
  50.  
  51. let s = str::from_utf8(&buff[..bytes_read - 2]).unwrap();
  52.  
  53. // Why does this allways error that the receiver is gone?
  54. sender.start_send(s.parse().unwrap()).map_err(|e| eprintln!("err {}", e));
  55. }
  56.  
  57. fn main() {
  58. tokio::run_async(async move {
  59. let (sender, recv) = unbounded::<Uri>();
  60.  
  61. tokio::spawn_async(sending(sender));
  62.  
  63. tokio::spawn_async(write_stdout(recv));
  64. });
  65. }
Add Comment
Please, Sign In to add comment