use rs_taskflow::flow::Flow; use rs_taskflow::task::*; // // Task that just simply outputs constant data // #[derive(Clone)] pub struct ZeroInputTwoOutputTask { output0: Option, output1: Option, func: F, } impl ZeroInputTwoOutputTask { pub fn new(task_func: F) -> ZeroInputTwoOutputTask { Self { output0: None, output1: None, func: task_func, } } } impl< O1: 'static + Clone + Send + Sync, O2: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn() -> (O1, O2), > TaskOutput0 for ZeroInputTwoOutputTask { fn get_output_0(task: &dyn ExecutableTask) -> Option<&O1> { task.as_any() .downcast_ref::() .unwrap() .output0 .as_ref() } } impl< O1: 'static + Clone + Send + Sync, O2: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn() -> (O1, O2), > TaskOutput1 for ZeroInputTwoOutputTask { fn get_output_1(task: &dyn ExecutableTask) -> Option<&O2> { task.as_any() .downcast_ref::() .unwrap() .output1 .as_ref() } } impl< O1: 'static + Clone + Send + Sync, O2: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn() -> (O1, O2), > ExecutableTask for ZeroInputTwoOutputTask { fn exec(&mut self, _flow: &Flow) { let (o1, o2) = (self.func)(); self.output0 = Some(o1); self.output1 = Some(o2); } } // // Task that just simply forwards data // #[derive(Clone)] pub struct OneInputOneOutputTask { input_handle: Option>, output: Option, func: F, } impl OneInputOneOutputTask { pub fn new(task_func: F) -> Self { Self { input_handle: None, output: None, func: task_func, } } } impl< I: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I) -> O, > TaskInput0 for OneInputOneOutputTask { fn set_input_0(&mut self, task_input: TaskInputHandle) { self.input_handle = Some(task_input); } } impl< I: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I) -> O, > TaskOutput0 for OneInputOneOutputTask { fn get_output_0(task: &dyn ExecutableTask) -> Option<&O> { task.as_any() .downcast_ref::() .unwrap() .output .as_ref() } } impl< I: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I) -> O, > ExecutableTask for OneInputOneOutputTask { fn exec(&mut self, flow: &Flow) { match &self.input_handle { Some(input) => { let input_val = input.get_value(flow); let o1 = (self.func)(input_val.unwrap()); self.output = Some(o1); } _ => { unreachable!(); } } } } // // Task that adds two numbers // #[derive(Clone)] pub struct TwoInputOneOutputTask { input0_handle: Option>, input1_handle: Option>, output: Option, func: F, } impl TwoInputOneOutputTask { pub fn new(task_func: F) -> Self { Self { input0_handle: None, input1_handle: None, output: None, func: task_func, } } } impl< I1: 'static + Clone, I2: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I1, &I2) -> O, > TaskInput0 for TwoInputOneOutputTask { fn set_input_0(&mut self, task_input: TaskInputHandle) { self.input0_handle = Some(task_input); } } impl< I1: 'static + Clone, I2: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I1, &I2) -> O, > TaskInput1 for TwoInputOneOutputTask { fn set_input_1(&mut self, task_input: TaskInputHandle) { self.input1_handle = Some(task_input); } } impl< I1: 'static + Clone, I2: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I1, &I2) -> O, > TaskOutput0 for TwoInputOneOutputTask { fn get_output_0(task: &dyn ExecutableTask) -> Option<&O> { task.as_any() .downcast_ref::() .unwrap() .output .as_ref() } } impl< I1: 'static + Clone, I2: 'static + Clone, O: 'static + Clone + Send + Sync, F: 'static + Clone + Send + Sync + Fn(&I1, &I2) -> O, > ExecutableTask for TwoInputOneOutputTask { fn exec(&mut self, flow: &Flow) { match (&self.input0_handle, &self.input1_handle) { (Some(input0), Some(input1)) => { let input0_val = input0.get_value(flow); let input1_val = input1.get_value(flow); let o1 = (self.func)(input0_val.unwrap(), input1_val.unwrap()); self.output = Some(o1); } _ => { unreachable!(); } } } }