diff --git a/axum/src/extract/nested_path.rs b/axum/src/extract/nested_path.rs index ba95b1d677..2e1628fa4e 100644 --- a/axum/src/extract/nested_path.rs +++ b/axum/src/extract/nested_path.rs @@ -6,7 +6,7 @@ use std::{ use async_trait::async_trait; use axum_core::extract::FromRequestParts; use http::{request::Parts, Request}; -use tower_layer::Layer; +use tower_layer::{Layer, layer_fn}; use tower_service::Service; use super::rejection::NestedPathRejection; @@ -62,33 +62,21 @@ where } #[derive(Clone)] -pub(crate) struct SetNestedPathLayer { +pub(crate) struct SetNestedPath { + inner: S, path: Arc, } -impl SetNestedPathLayer { - pub(crate) fn new(path: &str) -> Self { - Self { path: path.into() } - } -} - -impl Layer for SetNestedPathLayer { - type Service = SetNestedPath; - - fn layer(&self, inner: S) -> Self::Service { - SetNestedPath { +impl SetNestedPath { + pub(crate) fn layer(path: &str) -> impl Layer + Clone { + let path = Arc::from(path); + layer_fn(move |inner| Self { inner, - path: Arc::clone(&self.path), - } + path: Arc::clone(&path), + }) } } -#[derive(Clone)] -pub(crate) struct SetNestedPath { - inner: S, - path: Arc, -} - impl Service> for SetNestedPath where S: Service>, @@ -97,6 +85,7 @@ where type Error = S::Error; type Future = S::Future; + #[inline] fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.inner.poll_ready(cx) } diff --git a/axum/src/routing/path_router.rs b/axum/src/routing/path_router.rs index 9f1e8e1fed..6caa716367 100644 --- a/axum/src/routing/path_router.rs +++ b/axum/src/routing/path_router.rs @@ -1,6 +1,6 @@ use crate::{ body::{Body, HttpBody}, - extract::nested_path::SetNestedPathLayer, + extract::nested_path::SetNestedPath, }; use axum_core::response::IntoResponse; use http::Request; @@ -155,7 +155,7 @@ where let layer = ServiceBuilder::new() .layer(StripPrefix::layer(prefix)) - .layer(SetNestedPathLayer::new(prefix)) + .layer(SetNestedPath::layer(prefix)) .into_inner(); match endpoint.layer(layer) { @@ -188,7 +188,7 @@ where let layer = ServiceBuilder::new() .layer(StripPrefix::layer(prefix)) - .layer(SetNestedPathLayer::new(prefix)) + .layer(SetNestedPath::layer(prefix)) .into_inner(); let endpoint = Endpoint::Route(Route::new(layer.layer(svc)));