Skip to content

Commit 82b67f8

Browse files
committed
Replace Router::{map_inner, tap_inner_mut} by macros
… so that #[track_caller] works. Adding #[track_caller] to the two functions would not have the same effect as the closures passed to both would also need the attribute, but it's currently not supported on closures: rust-lang/rust#87417
1 parent 6318b57 commit 82b67f8

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

axum/src/routing/mod.rs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ pub(crate) const NEST_TAIL_PARAM_CAPTURE: &str = "/{*__private__axum_nest_tail_p
103103
pub(crate) const FALLBACK_PARAM: &str = "__private__axum_fallback";
104104
pub(crate) const FALLBACK_PARAM_PATH: &str = "/{*__private__axum_fallback}";
105105

106+
macro_rules! map_inner {
107+
( $self_:ident, $inner:pat_param => $expr:expr) => {
108+
#[allow(redundant_semicolons)]
109+
{
110+
let $inner = $self_.into_inner();
111+
Router {
112+
inner: Arc::new($expr),
113+
}
114+
}
115+
};
116+
}
117+
118+
macro_rules! tap_inner {
119+
( $self_:ident, mut $inner:ident => { $($stmt:stmt)* } ) => {
120+
#[allow(redundant_semicolons)]
121+
{
122+
let mut $inner = $self_.into_inner();
123+
$($stmt)*
124+
Router {
125+
inner: Arc::new($inner),
126+
}
127+
}
128+
};
129+
}
130+
106131
impl<S> Router<S>
107132
where
108133
S: Clone + Send + Sync + 'static,
@@ -122,26 +147,6 @@ where
122147
}
123148
}
124149

125-
fn map_inner<F, S2>(self, f: F) -> Router<S2>
126-
where
127-
F: FnOnce(RouterInner<S>) -> RouterInner<S2>,
128-
{
129-
Router {
130-
inner: Arc::new(f(self.into_inner())),
131-
}
132-
}
133-
134-
fn tap_inner_mut<F>(self, f: F) -> Self
135-
where
136-
F: FnOnce(&mut RouterInner<S>),
137-
{
138-
let mut inner = self.into_inner();
139-
f(&mut inner);
140-
Router {
141-
inner: Arc::new(inner),
142-
}
143-
}
144-
145150
fn into_inner(self) -> RouterInner<S> {
146151
match Arc::try_unwrap(self.inner) {
147152
Ok(inner) => inner,
@@ -156,15 +161,15 @@ where
156161

157162
#[doc = include_str!("../docs/routing/without_v07_checks.md")]
158163
pub fn without_v07_checks(self) -> Self {
159-
self.tap_inner_mut(|this| {
164+
tap_inner!(self, mut this => {
160165
this.path_router.without_v07_checks();
161166
})
162167
}
163168

164169
#[doc = include_str!("../docs/routing/route.md")]
165170
#[track_caller]
166171
pub fn route(self, path: &str, method_router: MethodRouter<S>) -> Self {
167-
self.tap_inner_mut(|this| {
172+
tap_inner!(self, mut this => {
168173
panic_on_err!(this.path_router.route(path, method_router));
169174
})
170175
}
@@ -186,7 +191,7 @@ where
186191
Err(service) => service,
187192
};
188193

189-
self.tap_inner_mut(|this| {
194+
tap_inner!(self, mut this => {
190195
panic_on_err!(this.path_router.route_service(path, service));
191196
})
192197
}
@@ -205,7 +210,7 @@ where
205210
catch_all_fallback: _,
206211
} = router.into_inner();
207212

208-
self.tap_inner_mut(|this| {
213+
tap_inner!(self, mut this => {
209214
panic_on_err!(this.path_router.nest(path, path_router));
210215

211216
if !default_fallback {
@@ -222,7 +227,7 @@ where
222227
T::Response: IntoResponse,
223228
T::Future: Send + 'static,
224229
{
225-
self.tap_inner_mut(|this| {
230+
tap_inner!(self, mut this => {
226231
panic_on_err!(this.path_router.nest_service(path, service));
227232
})
228233
}
@@ -244,7 +249,7 @@ where
244249
catch_all_fallback,
245250
} = other.into_inner();
246251

247-
self.map_inner(|mut this| {
252+
map_inner!(self, mut this => {
248253
panic_on_err!(this.path_router.merge(path_router));
249254

250255
match (this.default_fallback, default_fallback) {
@@ -288,7 +293,7 @@ where
288293
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
289294
<L::Service as Service<Request>>::Future: Send + 'static,
290295
{
291-
self.map_inner(|this| RouterInner {
296+
map_inner!(self, this => RouterInner {
292297
path_router: this.path_router.layer(layer.clone()),
293298
fallback_router: this.fallback_router.layer(layer.clone()),
294299
default_fallback: this.default_fallback,
@@ -306,7 +311,7 @@ where
306311
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
307312
<L::Service as Service<Request>>::Future: Send + 'static,
308313
{
309-
self.map_inner(|this| RouterInner {
314+
map_inner!(self, this => RouterInner {
310315
path_router: this.path_router.route_layer(layer),
311316
fallback_router: this.fallback_router,
312317
default_fallback: this.default_fallback,
@@ -326,7 +331,7 @@ where
326331
H: Handler<T, S>,
327332
T: 'static,
328333
{
329-
self.tap_inner_mut(|this| {
334+
tap_inner!(self, mut this => {
330335
this.catch_all_fallback =
331336
Fallback::BoxedHandler(BoxedIntoRoute::from_handler(handler.clone()));
332337
})
@@ -343,22 +348,22 @@ where
343348
T::Future: Send + 'static,
344349
{
345350
let route = Route::new(service);
346-
self.tap_inner_mut(|this| {
351+
tap_inner!(self, mut this => {
347352
this.catch_all_fallback = Fallback::Service(route.clone());
348353
})
349354
.fallback_endpoint(Endpoint::Route(route))
350355
}
351356

352357
fn fallback_endpoint(self, endpoint: Endpoint<S>) -> Self {
353-
self.tap_inner_mut(|this| {
358+
tap_inner!(self, mut this => {
354359
this.fallback_router.set_fallback(endpoint);
355360
this.default_fallback = false;
356361
})
357362
}
358363

359364
#[doc = include_str!("../docs/routing/with_state.md")]
360365
pub fn with_state<S2>(self, state: S) -> Router<S2> {
361-
self.map_inner(|this| RouterInner {
366+
map_inner!(self, this => RouterInner {
362367
path_router: this.path_router.with_state(state.clone()),
363368
fallback_router: this.fallback_router.with_state(state.clone()),
364369
default_fallback: this.default_fallback,

0 commit comments

Comments
 (0)