From 88604db0b6ffdaf93dfbdc11b0829097ff4bfca1 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 11 Feb 2024 12:06:03 +0100 Subject: [PATCH] Fix layers being cloned for each request --- axum/CHANGELOG.md | 5 ++++- axum/src/routing/method_routing.rs | 2 +- axum/src/routing/mod.rs | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index b3c8d3bedd..80ce95c379 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **fixed:** Fixed layers being cloned when calling `axum::serve` directly with + a `Router` or `MethodRouter` ([#2586]) + +[#2586]: https://github.com/tokio-rs/axum/pull/2586 # 0.7.4 (13. January, 2024) diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index bb6a1bc98e..355dda9904 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -1239,7 +1239,7 @@ const _: () = { } fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future { - std::future::ready(Ok(self.clone())) + std::future::ready(Ok(self.clone().with_state(()))) } } }; diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index fad920a46b..6564df7d62 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -492,7 +492,9 @@ const _: () = { } fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future { - std::future::ready(Ok(self.clone())) + // call `Router::with_state` such that everything is turned into `Route` eagerly + // rather than doing that per request + std::future::ready(Ok(self.clone().with_state(()))) } } };