mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-19 00:00:14 +02:00
Use extensions directly in from_request_parts (#3542)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use axum::extract::{Extension, FromRequestParts};
|
||||
use axum::extract::FromRequestParts;
|
||||
use http::request::Parts;
|
||||
|
||||
/// Cache results of other extractors.
|
||||
@@ -88,13 +88,12 @@ where
|
||||
type Rejection = T::Rejection;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
match Extension::<CachedEntry<T>>::from_request_parts(parts, state).await {
|
||||
Ok(Extension(CachedEntry(value))) => Ok(Self(value)),
|
||||
Err(_) => {
|
||||
let value = T::from_request_parts(parts, state).await?;
|
||||
parts.extensions.insert(CachedEntry(value.clone()));
|
||||
Ok(Self(value))
|
||||
}
|
||||
if let Some(value) = parts.extensions.get::<CachedEntry<T>>() {
|
||||
Ok(Self(value.0.clone()))
|
||||
} else {
|
||||
let value = T::from_request_parts(parts, state).await?;
|
||||
parts.extensions.insert(CachedEntry(value.clone()));
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user