From 410fd49aa969cab3ee79f140d5924b318e5b8df1 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 28 Sep 2022 11:06:15 +0200 Subject: [PATCH] Fix Cached as the last argument of a handler function (#1428) * Remove FromRequest impl for Cached * Add a test for Cached as the last argument of a handler function --- axum-extra/src/extract/cached.rs | 35 ++++++++------------------------ 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index 548a2562..03565c77 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -1,8 +1,8 @@ use axum::{ async_trait, - extract::{Extension, FromRequest, FromRequestParts}, + extract::{Extension, FromRequestParts}, }; -use http::{request::Parts, Request}; +use http::request::Parts; use std::ops::{Deref, DerefMut}; /// Cache results of other extractors. @@ -88,29 +88,6 @@ pub struct Cached(pub T); #[derive(Clone)] struct CachedEntry(T); -#[async_trait] -impl FromRequest for Cached -where - B: Send + 'static, - S: Send + Sync, - T: FromRequestParts + Clone + Send + Sync + 'static, -{ - type Rejection = T::Rejection; - - async fn from_request(req: Request, state: &S) -> Result { - let (mut parts, _) = req.into_parts(); - - match Extension::>::from_request_parts(&mut parts, state).await { - Ok(Extension(CachedEntry(value))) => Ok(Self(value)), - Err(_) => { - let value = T::from_request_parts(&mut parts, state).await?; - parts.extensions.insert(CachedEntry(value.clone())); - Ok(Self(value)) - } - } - } -} - #[async_trait] impl FromRequestParts for Cached where @@ -148,7 +125,7 @@ impl DerefMut for Cached { #[cfg(test)] mod tests { use super::*; - use axum::{extract::FromRequestParts, http::Request}; + use axum::{extract::FromRequestParts, http::Request, routing::get, Router}; use http::request::Parts; use std::{ convert::Infallible, @@ -195,4 +172,10 @@ mod tests { assert_eq!(first, second); } + + // Not a #[test], we just want to know this compiles + async fn _last_handler_argument() { + async fn handler(_: http::Method, _: Cached) {} + let _r: Router = Router::new().route("/", get(handler)); + } }