pipe_it/
handler.rs

1use crate::{Context, FromContext};
2use std::future::Future;
3
4
5
6/// A trait for functions that can be used as pipeline steps.
7pub trait Handler<I, O, Args>: Send + Sync + 'static {
8    fn call(&self, ctx: Context<I>) -> impl Future<Output = O> + Send;
9}
10
11macro_rules! impl_handler_for_fn {
12    ($($P:ident),+ $(,)?) => {
13        impl<F, I, O, Fut, $($P),+> Handler<I, O, ($($P,)+)> for F
14        where
15            I: Clone + Send + Sync + 'static,
16            O: Send + 'static,
17            F: Fn($($P),+) -> Fut + Send + Sync + 'static,
18            Fut: Future<Output = O> + Send,
19            $(
20                $P: FromContext<I>
21            ),+
22        {
23            fn call(&self, ctx: Context<I>) -> impl Future<Output = O> + Send {
24                async move {
25                    self($($P::from(ctx.clone()).await),+).await
26                }
27            }
28        }
29    };
30}
31
32impl_handler_for_fn!(I1);
33impl_handler_for_fn!(I1, I2);
34impl_handler_for_fn!(I1, I2, I3);
35impl_handler_for_fn!(I1, I2, I3, I4);
36impl_handler_for_fn!(I1, I2, I3, I4, I5);
37impl_handler_for_fn!(I1, I2, I3, I4, I5, I6);
38impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7);
39impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8);
40impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9);
41impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10);
42impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11);
43impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12);
44impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13);
45impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14);
46impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15);
47impl_handler_for_fn!(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16);
48