Skip to content

Commit

Permalink
WIP: got await working
Browse files Browse the repository at this point in the history
  • Loading branch information
alsuren committed Oct 7, 2018
1 parent 0008fd7 commit 4e85764
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/handlers/async_await/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"}
1 change: 1 addition & 0 deletions examples/handlers/async_await/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
24 changes: 16 additions & 8 deletions examples/handlers/async_await/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! A basic example showing the request components
#![feature(async_await, futures_api, await_macro)]

extern crate futures;
extern crate gotham;
Expand All @@ -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;
Expand All @@ -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<Future<Item = Vec<u8>, Error = HandlerError> + Send>;

Expand Down Expand Up @@ -83,14 +88,17 @@ fn sleep_handler(mut state: State) -> Box<HandlerFuture> {
// `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)
Expand Down

0 comments on commit 4e85764

Please sign in to comment.