Skip to content

Commit

Permalink
Implement FromRequestParts for Parts and Extensions
Browse files Browse the repository at this point in the history
Fixes #2320
  • Loading branch information
davidpdrsn committed Nov 23, 2023
1 parent 43b14a5 commit 19f6252
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 16 additions & 4 deletions axum-core/src/extract/request_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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())
}
}

Expand Down
3 changes: 3 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **breaking:** `impl<T> IntoResponse(Parts) for Extension<T>` now requires
`T: Clone`, as that is required by the http crate ([#1882])
- **added:** Add `axum::Json::from_bytes` ([#2244])
- **added:** Implement `FromRequestParts` for `http::request::Parts` ([#2328])
- **added:** Implement `FromRequestParts` for `http::Extensions` ([#2328])

[#1664]: https://github.com/tokio-rs/axum/pull/1664
[#1751]: https://github.com/tokio-rs/axum/pull/1751
Expand All @@ -91,6 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#2143]: https://github.com/tokio-rs/axum/pull/2143
[#2149]: https://github.com/tokio-rs/axum/pull/2149
[#2244]: https://github.com/tokio-rs/axum/pull/2244
[#2328]: https://github.com/tokio-rs/axum/pull/2328

# 0.6.17 (25. April, 2023)

Expand Down

0 comments on commit 19f6252

Please sign in to comment.