Skip to content

Commit

Permalink
clean up layer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Apr 19, 2023
1 parent 4617fa5 commit 172a74e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
31 changes: 10 additions & 21 deletions axum/src/extract/nested_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,33 +62,21 @@ where
}

#[derive(Clone)]
pub(crate) struct SetNestedPathLayer {
pub(crate) struct SetNestedPath<S> {
inner: S,
path: Arc<str>,
}

impl SetNestedPathLayer {
pub(crate) fn new(path: &str) -> Self {
Self { path: path.into() }
}
}

impl<S> Layer<S> for SetNestedPathLayer {
type Service = SetNestedPath<S>;

fn layer(&self, inner: S) -> Self::Service {
SetNestedPath {
impl<S> SetNestedPath<S> {
pub(crate) fn layer(path: &str) -> impl Layer<S, Service = Self> + 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<S> {
inner: S,
path: Arc<str>,
}

impl<S, B> Service<Request<B>> for SetNestedPath<S>
where
S: Service<Request<B>>,
Expand All @@ -97,6 +85,7 @@ where
type Error = S::Error;
type Future = S::Future;

#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
Expand Down
6 changes: 3 additions & 3 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 172a74e

Please sign in to comment.