@@ -10,7 +10,7 @@ use crate::http::{
10
10
Method ,
11
11
HeaderMap ,
12
12
Header ,
13
- uri:: { Segments , Origin } ,
13
+ uri:: Segments ,
14
14
Status ,
15
15
ext:: IntoOwned ,
16
16
} ;
@@ -267,6 +267,19 @@ impl<F> Rewriter for MapFile<F>
267
267
}
268
268
}
269
269
270
+ /// Helper trait to simplify standard rewrites
271
+ #[ doc( hidden) ]
272
+ pub trait FileMap : for < ' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync + ' static { }
273
+ impl < F > FileMap for F
274
+ where F : for < ' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync + ' static
275
+ { }
276
+ /// Helper trait to simplify standard rewrites
277
+ #[ doc( hidden) ]
278
+ pub trait FileFilter : Fn ( & File < ' _ , ' _ > , & Request < ' _ > ) -> bool + Send + Sync + ' static { }
279
+ impl < F > FileFilter for F
280
+ where F : Fn ( & File < ' _ , ' _ > , & Request < ' _ > ) -> bool + Send + Sync + ' static
281
+ { }
282
+
270
283
/// Prepends the provided path, to serve files from a directory.
271
284
///
272
285
/// You can use [`relative!`] to make a path relative to the crate root, rather
@@ -286,10 +299,7 @@ impl<F> Rewriter for MapFile<F>
286
299
///
287
300
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
288
301
/// non-panicing variant.
289
- pub fn dir_root ( path : impl AsRef < Path > )
290
- -> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > )
291
- -> FileResponse < ' p , ' h > + Send + Sync + ' static
292
- {
302
+ pub fn dir_root ( path : impl AsRef < Path > ) -> impl FileMap {
293
303
let path = path. as_ref ( ) ;
294
304
if !path. is_dir ( ) {
295
305
let path = path. display ( ) ;
@@ -319,10 +329,7 @@ pub fn dir_root(path: impl AsRef<Path>)
319
329
///
320
330
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
321
331
/// non-panicing variant.
322
- pub fn file_root ( path : impl AsRef < Path > )
323
- -> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > )
324
- -> FileResponse < ' p , ' h > + Send + Sync + ' static
325
- {
332
+ pub fn file_root ( path : impl AsRef < Path > ) -> impl FileMap {
326
333
let path = path. as_ref ( ) ;
327
334
if !path. exists ( ) {
328
335
let path = path. display ( ) ;
@@ -348,10 +355,7 @@ pub fn file_root(path: impl AsRef<Path>)
348
355
/// .map_file(file_root_permissive("/tmp/rocket"))
349
356
/// # }
350
357
/// ```
351
- pub fn file_root_permissive ( path : impl AsRef < Path > )
352
- -> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > )
353
- -> FileResponse < ' p , ' h > + Send + Sync + ' static
354
- {
358
+ pub fn file_root_permissive ( path : impl AsRef < Path > ) -> impl FileMap {
355
359
let path = path. as_ref ( ) . to_path_buf ( ) ;
356
360
move |f, _r| {
357
361
FileResponse :: File ( f. map_path ( |p| path. join ( p) ) )
@@ -420,9 +424,7 @@ pub fn normalize_dirs<'p, 'h>(file: File<'p, 'h>, req: &Request<'_>) -> FileResp
420
424
/// .map_file(index("index.html"))
421
425
/// # }
422
426
/// ```
423
- pub fn index ( index : & ' static str )
424
- -> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync
425
- {
427
+ pub fn index ( index : & ' static str ) -> impl FileMap {
426
428
move |f, _r| if f. path . is_dir ( ) {
427
429
FileResponse :: File ( f. map_path ( |p| p. join ( index) ) )
428
430
} else {
0 commit comments