swiftide_agents/tasks/
closures.rs1use async_trait::async_trait;
2
3use super::{
4 errors::NodeError,
5 node::{NodeArg, NodeId, TaskNode},
6};
7
8#[derive(Clone)]
11pub struct SyncClosureTaskNode<F, I, O>
12where
13 F: Fn(&I) -> Result<O, NodeError> + Send + Sync + Clone + 'static,
14{
15 pub f: F,
16 _phantom: std::marker::PhantomData<(I, O)>,
17}
18
19impl<F, I, O> SyncClosureTaskNode<F, I, O>
20where
21 F: Fn(&I) -> Result<O, NodeError> + Send + Sync + Clone + 'static,
22 I: NodeArg + Clone,
23 O: NodeArg + Clone,
24{
25 pub fn new(f: F) -> Self {
26 SyncClosureTaskNode {
27 f,
28 _phantom: std::marker::PhantomData,
29 }
30 }
31}
32
33#[async_trait]
34impl<F, I, O> TaskNode for SyncClosureTaskNode<F, I, O>
35where
36 F: Fn(&I) -> Result<O, NodeError> + Clone + Send + Sync + 'static,
37 I: NodeArg + Clone,
38 O: NodeArg + Clone,
39{
40 type Input = I;
41 type Output = O;
42 type Error = NodeError;
43
44 async fn evaluate(
45 &self,
46 _node_id: &NodeId<
47 dyn TaskNode<Input = Self::Input, Output = Self::Output, Error = Self::Error>,
48 >,
49 input: &Self::Input,
50 ) -> Result<Self::Output, Self::Error> {
51 (self.f)(input)
52 }
53}