Debian Codegraph @

third_party/rust/warp/examples/wrapping.rs

raw
1 #![deny(warnings)]
2 use warp::Filter;
3
4 fn hello_wrapper<F, T>(
5 filter: F,
6 ) -> impl Filter<Extract = (&'static str,)> + Clone + Send + Sync + 'static
7 where
8 F: Filter<Extract = (T,), Error = std::convert::Infallible> + Clone + Send + Sync + 'static,
9 F::Extract: warp::Reply,
10 {
11 warp::any()
12 .map(|| {
13 println!("before filter");
14 })
15 .untuple_one()
16 .and(filter)
17 .map(|_arg| "wrapped hello world")
18 }
19
20 #[tokio::main]
21 async fn main() {
22 // Match any request and return hello world!
23 let routes = warp::any()
24 .map(|| "hello world")
25 .boxed()
26 .recover(|_err| async { Ok("recovered") })
27 // wrap the filter with hello_wrapper
28 .with(warp::wrap_fn(hello_wrapper));
29
30 warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
31 }