bronzeflow_core/task/
mod.rs

1pub mod builder;
2pub mod dag;
3
4use crate::prelude::{Runnable, RuntimeJoinHandle};
5use std::sync::{Arc, Mutex};
6
7use crate::runtime::{BuildFromRunnable, SafeMetadata, SafeWrappedRunner, WrappedRunner};
8
9pub type TaskInfo = SafeWrappedRunner;
10
11impl BuildFromRunnable for TaskInfo {
12    type Type = TaskInfo;
13    fn build_from(
14        runnable: impl Runnable<Handle = RuntimeJoinHandle<()>> + Send + 'static,
15    ) -> TaskInfo {
16        SafeWrappedRunner(Arc::new(Mutex::new(WrappedRunner(Box::new(runnable)))))
17    }
18}
19
20#[derive(Clone)]
21pub struct WrappedTask {
22    pub(crate) task: TaskInfo,
23    pub(crate) meta: Option<SafeMetadata>,
24}
25
26#[derive(Clone)]
27pub enum RunnableHolder {
28    Task(WrappedTask),
29    Dag(dag::DAG),
30}
31
32impl RunnableHolder {
33    pub fn time_holder(&mut self) -> Option<SafeMetadata> {
34        match self {
35            RunnableHolder::Task(t) => t.meta.as_ref().map(Arc::clone),
36            RunnableHolder::Dag(d) => d.meta.as_ref().map(Arc::clone),
37        }
38    }
39}
40
41impl WrappedTask {
42    pub fn new(task: TaskInfo, meta: Option<SafeMetadata>) -> Self {
43        WrappedTask { task, meta }
44    }
45}
46
47pub trait TryIntoTask {
48    type TaskDetail;
49
50    fn try_into_task(self) -> TaskInfo;
51}
52//
53// impl<F: Fn() + Send + 'static + Clone> TryIntoTask for SyncFn<F> {
54//     type TaskDetail = WrappedRunner;
55//
56//     fn try_into_task(self) -> TaskInfo {
57//         SafeWrappedRunner(Arc::new(Mutex::new(WrappedRunner(Box::new(SyncFn(
58//             self.0,
59//         ))))))
60//     }
61// }
62//
63// #[cfg(feature = "async")]
64// impl<F: Fn() -> U + Send + Clone + 'static, U: std::future::Future + Send + 'static> TryIntoTask
65//     for AsyncFn<F, U>
66// {
67//     type TaskDetail = WrappedRunner;
68//
69//     fn try_into_task(self) -> TaskInfo {
70//         SafeWrappedRunner(Arc::new(Mutex::new(WrappedRunner(Box::new(AsyncFn(
71//             self.0,
72//         ))))))
73//     }
74// }
75//
76// impl<F: Fn() + Send + 'static + Clone> TryIntoTask for F {
77//     type TaskDetail = WrappedRunner;
78//
79//     fn try_into_task(self) -> TaskInfo {
80//         TryIntoTask::try_into_task(SyncFn(self))
81//     }
82// }
83
84// impl<F: Fn() + Send + 'static + Clone> ! NotFnRunnable for F {}
85
86impl<F> TryIntoTask for F
87where
88    F: Runnable<Handle = RuntimeJoinHandle<()>> + Send,
89{
90    type TaskDetail = WrappedRunner;
91
92    fn try_into_task(self) -> TaskInfo {
93        SafeWrappedRunner(Arc::new(Mutex::new(WrappedRunner(Box::new(self)))))
94    }
95}