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 -2
View File
@@ -1,5 +1,9 @@
use axum::response::IntoResponse;
use axum::{async_trait, extract::FromRequest, prelude::*};
use axum::{
async_trait,
extract::{FromRequest, RequestParts},
prelude::*,
};
use http::Response;
use http::StatusCode;
use std::net::SocketAddr;
@@ -36,7 +40,7 @@ where
{
type Rejection = Response<Body>;
async fn from_request(req: &mut Request<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = extract::UrlParamsMap::from_request(req)
.await
.map_err(IntoResponse::into_response)?;