when2task/exec/
mode.rs

1use std::future::Future;
2use std::pin::Pin;
3use tokio::task::JoinHandle;
4
5type ExecutionFn<T, E> = Box<
6    dyn Fn(Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'static>>) -> JoinHandle<Result<T, E>>
7        + Send
8        + 'static,
9>;
10
11use derive_getters::Getters;
12
13#[derive(Getters)]
14pub struct ExecutionMode<T, E> {
15    pub(crate) execution_fn: Option<ExecutionFn<T, E>>,
16}
17
18impl<T, E> ExecutionMode<T, E> {
19    /// Everything function is executed truly asynchronously
20    /// For example, if a step has tasks A, B and C, we execute
21    /// each of them asynchronously.
22    pub fn true_async() -> Self {
23        Self { execution_fn: None }
24    }
25
26    /// All the individual tasks in a step are executed in parallel,
27    /// but we wait for all the tasks in the same step to complete.
28    /// For example, if a step has tasks A, B and C, we execute
29    /// the tasks in parallel and wait for all of them.
30    pub fn pseudo_async<F>(execution_fn: F) -> Self
31    where
32        F: Fn(
33                Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'static>>,
34            ) -> JoinHandle<Result<T, E>>
35            + Send
36            + 'static,
37    {
38        Self {
39            execution_fn: Some(Box::new(execution_fn)),
40        }
41    }
42
43    /*pub fn parallel() -> Self {
44        todo!()
45    }*/
46}