avoid cloning the state in layer

This commit is contained in:
novacrazy 2024-10-10 11:35:04 +02:00 committed by Yann Simon
parent 2dc164b4f8
commit af95794e7d
3 changed files with 8 additions and 2 deletions

View file

@ -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

View file

@ -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))

View file

@ -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]