Skip to content

Commit f6733f6

Browse files
committed
remove one clone
1 parent c5967ee commit f6733f6

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

axum/src/routing/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,12 @@ where
668668
}
669669
}
670670

671-
fn call_with_state(&mut self, req: Request, state: S) -> RouteFuture<E> {
671+
fn call_with_state(self, req: Request, state: S) -> RouteFuture<E> {
672672
match self {
673-
Fallback::Default(route) | Fallback::Service(route) => route.oneshot_inner(req),
673+
Fallback::Default(route) | Fallback::Service(route) => route.oneshot_inner_owned(req),
674674
Fallback::BoxedHandler(handler) => {
675-
let mut route = handler.clone().into_route(state);
676-
route.oneshot_inner(req)
675+
let route = handler.clone().into_route(state);
676+
route.oneshot_inner_owned(req)
677677
}
678678
}
679679
}

axum/src/routing/route.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ impl<E> Route<E> {
4747
RouteFuture::new(method, self.0.clone().oneshot(req))
4848
}
4949

50+
/// Variant of [`Route::oneshot_inner`] that takes ownership of the route to avoid cloning.
51+
pub(crate) fn oneshot_inner_owned(self, req: Request) -> RouteFuture<E> {
52+
let method = req.method().clone();
53+
RouteFuture::new(method, self.0.oneshot(req))
54+
}
55+
5056
pub(crate) fn layer<L, NewError>(self, layer: L) -> Route<NewError>
5157
where
5258
L: Layer<Route<E>> + Clone + Send + 'static,

axum/src/routing/tests/fallback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,5 @@ async fn state_isnt_cloned_too_much_with_fallback() {
344344

345345
client.get("/does-not-exist").await;
346346

347-
assert_eq!(state.count(), 4);
347+
assert_eq!(state.count(), 3);
348348
}

0 commit comments

Comments
 (0)