Make extractors easier to write (#36)

Previously extractors worked directly on `Request<B>` which meant you
had to do weird tricks like `mem::take(req.headers_mut())` to get owned
parts of the request.

This changes that instead to use a new `RequestParts` type that have
methods to "take" each part of the request. Without having to do weird
tricks.

Also removed the need to have `B: Default` for body extractors.
This commit is contained in:
David Pedersen
2021-07-22 13:23:50 +02:00
committed by GitHub
parent e544fe1c39
commit f32d325e55
12 changed files with 441 additions and 193 deletions
+8 -3
View File
@@ -1,6 +1,11 @@
//! Routing between [`Service`]s.
use crate::{body::BoxBody, buffer::MpscBuffer, response::IntoResponse, util::ByteStr};
use crate::{
body::{box_body, BoxBody},
buffer::MpscBuffer,
response::IntoResponse,
util::ByteStr,
};
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::future;
@@ -165,7 +170,7 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized {
.layer_fn(BoxRoute)
.layer_fn(MpscBuffer::new)
.layer(BoxService::layer())
.layer(MapResponseBodyLayer::new(BoxBody::new))
.layer(MapResponseBodyLayer::new(box_body))
.service(self)
}
@@ -399,7 +404,7 @@ impl<B, E> Service<Request<B>> for EmptyRouter<E> {
}
fn call(&mut self, _req: Request<B>) -> Self::Future {
let mut res = Response::new(BoxBody::empty());
let mut res = Response::new(crate::body::empty());
*res.status_mut() = StatusCode::NOT_FOUND;
EmptyRouterFuture(future::ok(res))
}