@@ -103,6 +103,31 @@ pub(crate) const NEST_TAIL_PARAM_CAPTURE: &str = "/{*__private__axum_nest_tail_p
103
103
pub ( crate ) const FALLBACK_PARAM : & str = "__private__axum_fallback" ;
104
104
pub ( crate ) const FALLBACK_PARAM_PATH : & str = "/{*__private__axum_fallback}" ;
105
105
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
+
106
131
impl < S > Router < S >
107
132
where
108
133
S : Clone + Send + Sync + ' static ,
@@ -122,26 +147,6 @@ where
122
147
}
123
148
}
124
149
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
-
145
150
fn into_inner ( self ) -> RouterInner < S > {
146
151
match Arc :: try_unwrap ( self . inner ) {
147
152
Ok ( inner) => inner,
@@ -156,15 +161,15 @@ where
156
161
157
162
#[ doc = include_str ! ( "../docs/routing/without_v07_checks.md" ) ]
158
163
pub fn without_v07_checks ( self ) -> Self {
159
- self . tap_inner_mut ( | this| {
164
+ tap_inner ! ( self , mut this => {
160
165
this. path_router. without_v07_checks( ) ;
161
166
} )
162
167
}
163
168
164
169
#[ doc = include_str ! ( "../docs/routing/route.md" ) ]
165
170
#[ track_caller]
166
171
pub fn route ( self , path : & str , method_router : MethodRouter < S > ) -> Self {
167
- self . tap_inner_mut ( | this| {
172
+ tap_inner ! ( self , mut this => {
168
173
panic_on_err!( this. path_router. route( path, method_router) ) ;
169
174
} )
170
175
}
@@ -186,7 +191,7 @@ where
186
191
Err ( service) => service,
187
192
} ;
188
193
189
- self . tap_inner_mut ( | this| {
194
+ tap_inner ! ( self , mut this => {
190
195
panic_on_err!( this. path_router. route_service( path, service) ) ;
191
196
} )
192
197
}
@@ -205,7 +210,7 @@ where
205
210
catch_all_fallback : _,
206
211
} = router. into_inner ( ) ;
207
212
208
- self . tap_inner_mut ( | this| {
213
+ tap_inner ! ( self , mut this => {
209
214
panic_on_err!( this. path_router. nest( path, path_router) ) ;
210
215
211
216
if !default_fallback {
@@ -222,7 +227,7 @@ where
222
227
T :: Response : IntoResponse ,
223
228
T :: Future : Send + ' static ,
224
229
{
225
- self . tap_inner_mut ( | this| {
230
+ tap_inner ! ( self , mut this => {
226
231
panic_on_err!( this. path_router. nest_service( path, service) ) ;
227
232
} )
228
233
}
@@ -244,7 +249,7 @@ where
244
249
catch_all_fallback,
245
250
} = other. into_inner ( ) ;
246
251
247
- self . map_inner ( | mut this| {
252
+ map_inner ! ( self , mut this => {
248
253
panic_on_err!( this. path_router. merge( path_router) ) ;
249
254
250
255
match ( this. default_fallback, default_fallback) {
@@ -288,7 +293,7 @@ where
288
293
<L :: Service as Service < Request > >:: Error : Into < Infallible > + ' static ,
289
294
<L :: Service as Service < Request > >:: Future : Send + ' static ,
290
295
{
291
- self . map_inner ( | this| RouterInner {
296
+ map_inner ! ( self , this => RouterInner {
292
297
path_router: this. path_router. layer( layer. clone( ) ) ,
293
298
fallback_router: this. fallback_router. layer( layer. clone( ) ) ,
294
299
default_fallback: this. default_fallback,
@@ -306,7 +311,7 @@ where
306
311
<L :: Service as Service < Request > >:: Error : Into < Infallible > + ' static ,
307
312
<L :: Service as Service < Request > >:: Future : Send + ' static ,
308
313
{
309
- self . map_inner ( | this| RouterInner {
314
+ map_inner ! ( self , this => RouterInner {
310
315
path_router: this. path_router. route_layer( layer) ,
311
316
fallback_router: this. fallback_router,
312
317
default_fallback: this. default_fallback,
@@ -326,7 +331,7 @@ where
326
331
H : Handler < T , S > ,
327
332
T : ' static ,
328
333
{
329
- self . tap_inner_mut ( | this| {
334
+ tap_inner ! ( self , mut this => {
330
335
this. catch_all_fallback =
331
336
Fallback :: BoxedHandler ( BoxedIntoRoute :: from_handler( handler. clone( ) ) ) ;
332
337
} )
@@ -343,22 +348,22 @@ where
343
348
T :: Future : Send + ' static ,
344
349
{
345
350
let route = Route :: new ( service) ;
346
- self . tap_inner_mut ( | this| {
351
+ tap_inner ! ( self , mut this => {
347
352
this. catch_all_fallback = Fallback :: Service ( route. clone( ) ) ;
348
353
} )
349
354
. fallback_endpoint ( Endpoint :: Route ( route) )
350
355
}
351
356
352
357
fn fallback_endpoint ( self , endpoint : Endpoint < S > ) -> Self {
353
- self . tap_inner_mut ( | this| {
358
+ tap_inner ! ( self , mut this => {
354
359
this. fallback_router. set_fallback( endpoint) ;
355
360
this. default_fallback = false ;
356
361
} )
357
362
}
358
363
359
364
#[ doc = include_str ! ( "../docs/routing/with_state.md" ) ]
360
365
pub fn with_state < S2 > ( self , state : S ) -> Router < S2 > {
361
- self . map_inner ( | this| RouterInner {
366
+ map_inner ! ( self , this => RouterInner {
362
367
path_router: this. path_router. with_state( state. clone( ) ) ,
363
368
fallback_router: this. fallback_router. with_state( state. clone( ) ) ,
364
369
default_fallback: this. default_fallback,
0 commit comments