Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #[macro_use]
  2. extern crate futures;
  3. extern crate tokio;
  4.  
  5.  
  6. // `Poll` is a type alias for `Result<Async<T>, E>`
  7. use futures::{Future, Async, Poll};
  8. use std::fmt;
  9.  
  10.  
  11.  
  12. struct HelloWorld;
  13.  
  14. impl Future for HelloWorld {
  15. type Item = String;
  16. type Error = ();
  17.  
  18. fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
  19. Ok(Async::Ready("hello world".to_string()))
  20. }
  21. }
  22.  
  23.  
  24. struct Display<T>(T);
  25.  
  26. impl<T> Future for Display<T>
  27. where
  28. T: Future,
  29. T::Item: fmt::Display,
  30. {
  31. type Item = ();
  32. type Error = T::Error;
  33.  
  34. fn poll(&mut self) -> Poll<(), T::Error> {
  35. let value = try_ready!(self.0.poll());
  36. println!("{}", value);
  37. Ok(Async::Ready(()))
  38. }
  39. }
  40.  
  41.  
  42. fn main () {
  43. let future = Display(HelloWorld);
  44. tokio::run(future);
  45.  
  46. println!("done");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement