nightly actix-web-async-await

Provides a preview of Actix with async/await support

3 unstable releases

0.2.0 Nov 6, 2018
0.1.1 Oct 26, 2018
0.1.0 Oct 26, 2018

#1282 in Asynchronous

MIT/Apache

7KB

Actix Web Async/Await Preview

This crate provides a preview of Actix with async/await support.

Usage

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.

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~31MB
~487K SLoC