Skip to content

Commit 19f6252

Browse files
committed
Implement FromRequestParts for Parts and Extensions
Fixes #2320
1 parent 43b14a5 commit 19f6252

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

axum-core/src/extract/request_parts.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{rejection::*, FromRequest, FromRequestParts, Request};
22
use crate::{body::Body, RequestExt};
33
use async_trait::async_trait;
44
use bytes::Bytes;
5-
use http::{request::Parts, HeaderMap, Method, Uri, Version};
5+
use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version};
66
use http_body_util::BodyExt;
77
use std::convert::Infallible;
88

@@ -115,14 +115,26 @@ where
115115
}
116116

117117
#[async_trait]
118-
impl<S> FromRequest<S> for Parts
118+
impl<S> FromRequestParts<S> for Parts
119119
where
120120
S: Send + Sync,
121121
{
122122
type Rejection = Infallible;
123123

124-
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
125-
Ok(req.into_parts().0)
124+
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
125+
Ok(parts.clone())
126+
}
127+
}
128+
129+
#[async_trait]
130+
impl<S> FromRequestParts<S> for Extensions
131+
where
132+
S: Send + Sync,
133+
{
134+
type Rejection = Infallible;
135+
136+
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
137+
Ok(parts.extensions.clone())
126138
}
127139
}
128140

axum/CHANGELOG.md

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

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

9598
# 0.6.17 (25. April, 2023)
9699

0 commit comments

Comments
 (0)