-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
63 lines (59 loc) · 1.35 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::future::Future;
macro_rules! compose_middleware_inner {
( $route:ident, $first:ident, $second:ident, $($tail:ident), +) => {
$first(|| async {
compose_middleware_inner!($route, $second, $($tail),+)
}).await
};
( $route: ident, $first:ident, $second:ident ) => {
$first(|| async move { $second($route).await }).await
};
}
/// Replacing the macro with its expanded form still results in huge compile times.
/// The macro will generate a function that looks like this:
///
/// pub async fn my_middleware<N, Fut>(route: N)
/// where
/// N: FnOnce() -> Fut,
/// Fut: Future<Output = ()>,
/// {
/// log(|| async { log(|| async { log(|| async move { log(route).await }).await }).await }).await
/// }
macro_rules! compose_middleware {
( $name:ident, $($tail:ident), +) => {
pub async fn $name<N, Fut>(route: N)
where
N: FnOnce() -> Fut,
Fut: Future<Output = ()>,
{
compose_middleware_inner!(route, $($tail),+)
}
}
}
async fn log<N, Fut>(next: N)
where
N: FnOnce() -> Fut,
Fut: Future<Output = ()>,
{
println!("log start");
next().await;
println!("log end");
}
compose_middleware!(
my_middleware,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log,
log
);