Crates.io | actix-web-async-await |
lib.rs | actix-web-async-await |
version | 0.2.0 |
source | src |
created_at | 2018-10-26 08:06:49.634536 |
updated_at | 2018-11-06 03:25:50.729456 |
description | Provides a preview of Actix with async/await support. |
homepage | |
repository | https://github.com/mehcode/actix-web-async-await |
max_upload_size | |
id | 92731 |
size | 17,749 |
This crate provides a preview of Actix with async/await support.
To use this crate, you need to start with a Rust 2018 edition crate.
Add this to your Cargo.toml:
# In the `[package]` section
edition = "2018"
# In the `[dependencies]` section
actix-web-async-await = "0.1.0"
Then, get started. Here is the headline Actix example with the addition that it asynchronously delays the request by 2 seconds.
The general idea is to wrap your async fn
handlers in compat
. There are compat2
, compat3
, etc. for routes taking multiple arguments.
#![feature(await_macro, futures_api, async_await)]
use actix_web::{http, server, App, Path, Responder, Result};
use actix_web_async_await::{await, compat};
use std::time::{Instant, Duration};
use tokio::timer::Delay;
async fn index(info: Path<(u32, String)>) -> Result<impl Responder> {
// Wait 2s
await!(Delay::new(Instant::now() + Duration::from_secs(2)))?;
// Proceed with normal response
Ok(format!("Hello {}! id:{}", info.1, info.0))
}
fn main() {
server::new(
|| App::new()
.route("/{id}/{name}/index.html", http::Method::GET, compat(index)))
.bind("127.0.0.1:8080").unwrap()
.run();
}
Note that your async fn
handlers must return Result
currently. This is both because they are being converted to futures v0.1
which requires an error type and because nearly the entire rust async ecosystem uses futures v0.1
which is going to have errors on all futures. An error-less compat
could be provided if the support is wanted.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.