mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 22:56:46 +01:00
avoid cloning the state in layer
This commit is contained in:
parent
2dc164b4f8
commit
af95794e7d
3 changed files with 8 additions and 2 deletions
|
@ -394,7 +394,7 @@ where
|
|||
Endpoint::MethodRouter(method_router) => {
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
self.oneshot_inner_owned(req).not_top_level()
|
||||
}
|
||||
|
||||
pub(crate) fn oneshot_inner(&mut self, req: Request) -> RouteFuture<E> {
|
||||
let method = req.method().clone();
|
||||
RouteFuture::new(method, self.0.clone().oneshot(req))
|
||||
|
|
|
@ -942,7 +942,7 @@ async fn state_isnt_cloned_too_much_in_layer() {
|
|||
|
||||
client.get("/").await;
|
||||
|
||||
assert_eq!(state.count(), 4);
|
||||
assert_eq!(state.count(), 3);
|
||||
}
|
||||
|
||||
#[crate::test]
|
||||
|
|
Loading…
Reference in a new issue