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
+6 -3
View File
@@ -1,6 +1,9 @@
//! [`Service`](tower::Service) future types.
use crate::{body::BoxBody, response::IntoResponse};
use crate::{
body::{box_body, BoxBody},
response::IntoResponse,
};
use bytes::Bytes;
use futures_util::ready;
use http::Response;
@@ -36,11 +39,11 @@ where
let this = self.project();
match ready!(this.inner.poll(cx)) {
Ok(res) => Ok(res.map(BoxBody::new)).into(),
Ok(res) => Ok(res.map(box_body)).into(),
Err(err) => {
let f = this.f.take().unwrap();
let res = f(err).into_response();
Ok(res.map(BoxBody::new)).into()
Ok(res.map(box_body)).into()
}
}
}