1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 01:51:23 +02:00

rename async to a

This commit is contained in:
Nikolay Kim
2017-12-04 16:09:22 -08:00
parent e98972e93b
commit f4e9fc7b6a
7 changed files with 137 additions and 110 deletions

View File

@ -96,31 +96,50 @@ There are two different types of async handlers.
Response object could be generated asynchronously. In this case handle must
return `Future` object that resolves to `HttpResponse`, i.e:
```rust,ignore
fn index(req: HttpRequest) -> Box<Future<HttpResponse, Error>> {
...
}
```
```rust
# extern crate actix_web;
# extern crate futures;
# extern crate bytes;
# use actix_web::*;
# use bytes::Bytes;
# use futures::stream::once;
# use futures::future::{FutureResult, result};
fn index(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
This handler can be registered with `ApplicationBuilder::async()` and
`Resource::async()` methods.
Or response body can be generated asynchronously. In this case body
must implement stream trait `Stream<Item=Bytes, Error=Error>`, i.e:
```rust,ignore
fn index(req: HttpRequest) -> HttpResponse {
let body: Box<Stream<Item=Bytes, Error=Error>> = Box::new(SomeStream::new());
HttpResponse::Ok().
.content_type("application/json")
.body(Body::Streaming(body)).unwrap()
result(HttpResponse::Ok()
.content_type("text/html")
.body(format!("Hello!"))
.map_err(|e| e.into()))
}
fn main() {
Application::default("/")
.async("/async", index)
.route("/async", |r| r.a(index))
.finish();
}
```
Or response body can be generated asynchronously. In this case body
must implement stream trait `Stream<Item=Bytes, Error=Error>`, i.e:
```rust
# extern crate actix_web;
# extern crate futures;
# extern crate bytes;
# use actix_web::*;
# use bytes::Bytes;
# use futures::stream::once;
fn index(req: HttpRequest) -> HttpResponse {
let body = once(Ok(Bytes::from_static(b"test")));
HttpResponse::Ok()
.content_type("application/json")
.body(Body::Streaming(Box::new(body))).unwrap()
}
fn main() {
Application::default("/")
.route("/async", |r| r.f(index))
.finish();
}
```