diff --git a/Cargo.toml b/Cargo.toml index 1f73b717d..b7c921ea9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ members = [ "examples/handlers/request_data", "examples/handlers/stateful", "examples/handlers/simple_async_handlers", + "examples/handlers/async_await", # "examples/handlers/async_handlers", # static_assets diff --git a/examples/handlers/async_await/Cargo.toml b/examples/handlers/async_await/Cargo.toml index 9ff1b33bf..17f204328 100644 --- a/examples/handlers/async_await/Cargo.toml +++ b/examples/handlers/async_await/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gotham_examples_handlers_simple_async_handlers" +name = "gotham_examples_handlers_async_await" edition = "2018" description = "An example that does asynchronous work before responding" version = "0.0.0" @@ -19,4 +19,5 @@ mime = "0.3" futures = "0.1" serde = "1.0" serde_derive = "1.0" -tokio = "0.1" +tokio = {version = "0.1", features = ["async-await-preview"] } +tokio-async-await = { version = "0.1"} \ No newline at end of file diff --git a/examples/handlers/async_await/rust-toolchain b/examples/handlers/async_await/rust-toolchain new file mode 100644 index 000000000..bf867e0ae --- /dev/null +++ b/examples/handlers/async_await/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/examples/handlers/async_await/src/main.rs b/examples/handlers/async_await/src/main.rs index ae082d61f..9717b8837 100644 --- a/examples/handlers/async_await/src/main.rs +++ b/examples/handlers/async_await/src/main.rs @@ -1,4 +1,5 @@ //! A basic example showing the request components +#![feature(async_await, futures_api, await_macro)] extern crate futures; extern crate gotham; @@ -9,12 +10,15 @@ extern crate mime; extern crate serde; #[macro_use] extern crate serde_derive; +#[macro_use] extern crate tokio; +extern crate tokio_async_await; use futures::{stream, Future, Stream}; use std::time::{Duration, Instant}; use hyper::StatusCode; +use hyper::{Body, Response}; use gotham::handler::{HandlerError, HandlerFuture, IntoHandlerError}; use gotham::helpers::http::response::create_response; @@ -24,6 +28,7 @@ use gotham::router::Router; use gotham::state::{FromState, State}; use tokio::timer::Delay; +use tokio_async_await::compat::backward; type SleepFuture = Box, Error = HandlerError> + Send>; @@ -83,14 +88,17 @@ fn sleep_handler(mut state: State) -> Box { // `state` is moved in, so that we can return it, and we convert any errors // that we have into the form that Hyper expects, using the helper from // IntoHandlerError. - Box::new(sleep_future.then(move |result| match result { - Ok(data) => { - let res = create_response(&state, StatusCode::OK, (data, mime::TEXT_PLAIN)); - println!("sleep for {} seconds once: finished", seconds); - Ok((state, res)) - } - Err(err) => Err((state, err.into_handler_error())), - })) + let f = async move { + match await!(sleep_future) { + Ok(data) => { + let res = create_response(&state, StatusCode::OK, (data, mime::TEXT_PLAIN)); + println!("sleep for {} seconds once: finished", seconds); + Ok((state, res)) + } + Err(err) => Err((state, err.into_handler_error())), + } + }; + Box::new(backward::Compat::new(f)) } /// This example uses a `future::Stream` to implement a `for` loop. It calls sleep(1)