tosic_http/traits/
handler.rs

1//! Handler trait defines the signature of a handler function
2//! that can be used to implement a service handler
3
4use std::future::Future;
5
6#[diagnostic::on_unimplemented(
7    message = "Check the function signature and make sure it matches the format `async fn(..) -> impl Responder<Body = tosic_http::body::BoxBody>`",
8    label = "Used here",
9    note = "The above format is valid with up to 26 parameters where each parameter implements the `FromRequest` trait"
10)]
11/// # Handler trait
12///
13/// The Handler trait defines the signature of a handler function
14/// that can be used to implement a service handler
15pub trait Handler<Args>: 'static {
16    /// The output type of the handler
17    type Output;
18    /// The future type of the handler
19    type Future: Future<Output = Self::Output>;
20
21    /// Calls the handler function with the given arguments
22    fn call(&self, args: Args) -> Self::Future;
23}
24
25macro_rules! handler_tuple ({ $($param:ident)* } => {
26    impl<Func, Fut, $($param,)*> Handler<($($param,)*)> for Func
27    where
28        Func: Fn($($param),*) -> Fut + 'static,
29        Fut: Future,
30    {
31        type Output = Fut::Output;
32        type Future = Fut;
33
34        #[inline]
35        #[allow(non_snake_case)]
36        fn call(&self, ($($param,)*): ($($param,)*)) -> Self::Future {
37            (self)($($param,)*)
38        }
39    }
40});
41
42handler_tuple! {}
43handler_tuple! { A }
44handler_tuple! { A B }
45handler_tuple! { A B C }
46handler_tuple! { A B C D }
47handler_tuple! { A B C D E }
48handler_tuple! { A B C D E F }
49handler_tuple! { A B C D E F G }
50handler_tuple! { A B C D E F G H }
51handler_tuple! { A B C D E F G H I }
52handler_tuple! { A B C D E F G H I J }
53handler_tuple! { A B C D E F G H I J K }
54handler_tuple! { A B C D E F G H I J K L }
55handler_tuple! { A B C D E F G H I J K L M }
56handler_tuple! { A B C D E F G H I J K L M N }
57handler_tuple! { A B C D E F G H I J K L M N O }
58handler_tuple! { A B C D E F G H I J K L M N O P }
59handler_tuple! { A B C D E F G H I J K L M N O P Q }
60handler_tuple! { A B C D E F G H I J K L M N O P Q R }
61handler_tuple! { A B C D E F G H I J K L M N O P Q R S }
62handler_tuple! { A B C D E F G H I J K L M N O P Q R S T }
63handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U }
64handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U V }
65handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W }
66handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X }
67handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X Y }
68handler_tuple! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }