Implement FromRequestParts for Parts and Extensions (#2328)

This commit is contained in:
David Pedersen
2023-11-23 11:30:23 +00:00
committed by GitHub
parent 43b14a5f02
commit ff9764574c
4 changed files with 22 additions and 7 deletions
+16 -4
View File
@@ -2,7 +2,7 @@ use super::{rejection::*, FromRequest, FromRequestParts, Request};
use crate::{body::Body, RequestExt};
use async_trait::async_trait;
use bytes::Bytes;
use http::{request::Parts, HeaderMap, Method, Uri, Version};
use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version};
use http_body_util::BodyExt;
use std::convert::Infallible;
@@ -115,14 +115,26 @@ where
}
#[async_trait]
impl<S> FromRequest<S> for Parts
impl<S> FromRequestParts<S> for Parts
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
Ok(req.into_parts().0)
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Ok(parts.clone())
}
}
#[async_trait]
impl<S> FromRequestParts<S> for Extensions
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Ok(parts.extensions.clone())
}
}