add futures-based hello world

This commit is contained in:
2019-01-23 12:47:28 +01:00
parent f083abaf6d
commit 20f81caa1e
4 changed files with 714 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#[macro_use]
extern crate futures;
extern crate tokio;
use futures::{Future, Async, Poll};
use std::fmt;
struct HelloWorld;
impl Future for HelloWorld {
type Item = String;
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready("hello world".to_string()))
}
}
struct Display<T>(T);
impl<T> Future for Display<T>
where
T: Future,
T::Item: fmt::Display,
{
type Item = ();
type Error = T::Error;
fn poll(&mut self) -> Poll<(), T::Error> {
let value = try_ready!(self.0.poll());
println!("{}", value);
Ok(Async::Ready(()))
}
}
fn main() {
let future = Display(HelloWorld);
tokio::run(future);
}