Avoid cloning the state in layer

This commit is contained in:
novacrazy 2024-10-10 11:35:04 +02:00 committed by Jonas Platte
parent 296dfe1a40
commit c417a28142
No known key found for this signature in database
GPG key ID: 7D261D771D915378
3 changed files with 8 additions and 2 deletions

View file

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

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);
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,

View file

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