mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-23 07:39:25 +01:00
Avoid cloning the state in layer
This commit is contained in:
parent
296dfe1a40
commit
c417a28142
3 changed files with 8 additions and 2 deletions
|
@ -357,7 +357,7 @@ where
|
||||||
Endpoint::MethodRouter(method_router) => {
|
Endpoint::MethodRouter(method_router) => {
|
||||||
Ok(method_router.call_with_state(req, state))
|
Ok(method_router.call_with_state(req, state))
|
||||||
}
|
}
|
||||||
Endpoint::Route(route) => Ok(route.clone().call(req)),
|
Endpoint::Route(route) => Ok(route.clone().call_owned(req)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// explicitly handle all variants in case matchit adds
|
// explicitly handle all variants in case matchit adds
|
||||||
|
|
|
@ -42,6 +42,12 @@ impl<E> Route<E> {
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Variant of [`Route::call`] that takes ownership of the route to avoid cloning.
|
||||||
|
pub(crate) fn call_owned(self, req: Request<Body>) -> RouteFuture<E> {
|
||||||
|
let req = req.map(Body::new);
|
||||||
|
RouteFuture::from_future(self.oneshot_inner_owned(req))
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn oneshot_inner(
|
pub(crate) fn oneshot_inner(
|
||||||
&mut self,
|
&mut self,
|
||||||
req: Request,
|
req: Request,
|
||||||
|
|
|
@ -940,7 +940,7 @@ async fn state_isnt_cloned_too_much_in_layer() {
|
||||||
|
|
||||||
client.get("/").await;
|
client.get("/").await;
|
||||||
|
|
||||||
assert_eq!(state.count(), 4);
|
assert_eq!(state.count(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[crate::test]
|
#[crate::test]
|
||||||
|
|
Loading…
Reference in a new issue