spacegate_kernel/helper_layers/function/
handler.rs1#![allow(non_snake_case)]
2use futures_util::Future;
3use hyper::{Request, Response};
4
5use crate::SgBody;
6
7use super::Inner;
8use crate::extractor::Extract;
9pub trait HandlerFn<T, Fut>
12where
13 Fut: Future<Output = Response<SgBody>> + Send + 'static,
14{
15 fn apply(&self, request: Request<SgBody>, inner: Inner) -> Fut;
16}
17
18macro_rules! impl_handler {
19 ($($t:ident,)*) => {
21 impl_handler!(@);
22 impl_handler!(; $($t,)*);
23 };
24 ($($t:ident,)*;$next:ident, $($rest:ident,)*) => {
26 impl_handler!(@$($t,)*$next,);
27 impl_handler!($($t,)*$next,;$($rest,)*);
28 };
29 ($($t:ident,)*;) => {};
31 (@$($t:ident,)*) => {
33 #[allow(unused_variables, unused_parens)]
34 impl<F, Fut, $($t),* > HandlerFn<($($t),*), Fut> for F
35 where
36 F: (Fn(Request<SgBody>, Inner, $($t,)*) -> Fut) + Send + Sync + Clone + 'static ,
37 Fut: Future<Output = Response<SgBody>> + Send + 'static,
38 $($t: Extract),*
39 {
40 fn apply(&self, request: Request<SgBody>, inner: Inner) -> Fut {
41 $(let $t = $t::extract(&request);)*
42 (self.clone())(request, inner, $($t,)*)
43 }
44 }
45 };
46
47}
48
49impl Inner {
50 pub fn invoke<T, Fut, H>(self, request: Request<SgBody>, handler: H) -> Fut
51 where
52 H: HandlerFn<T, Fut>,
53 Fut: Future<Output = Response<SgBody>> + Send + 'static,
54 {
55 handler.apply(request, self)
56 }
57}
58
59impl_handler!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,);