pub struct ParallelFlow<Input, Output, Error, Context, ChainOutput = (), Joiner = (), NodeTypes = (), NodeIOETypes = ()> { /* private fields */ }Expand description
ParallelFlow executes nodes (branches) in parallel.
Nodes (branches) are executed concurrently. The flow completes when all node succeed or any node “hard” fails.
- If a node returns
NodeOutput::OkorNodeOutput::SoftFail, the flow continues waiting for other nodes (branches). - If a node returns an error, then that error is returned.
The output of all nodes is then passed into a Joiner,
which decides what should happen and what should this flow return.
§Type Parameters
Input: The type of data accepted by this flow.Output: The type of data produced by this flow.Error: The type of error emitted by this flow.Context: The type of context used during execution.
See also Joiner.
§Examples
use node_flow::node::{Node, NodeOutput};
use node_flow::flows::ParallelFlow;
use node_flow::context::{Fork, Join};
// Example nodes
#[derive(Clone)]
struct A;
#[derive(Clone)]
struct B;
struct ExampleCtx;
impl Fork for ExampleCtx // ...
impl Join for ExampleCtx // ...
impl<Ctx: Send> Node<(), NodeOutput<i32>, (), Ctx> for A {
async fn run(&mut self, _: (), _: &mut Ctx) -> Result<NodeOutput<i32>, ()> {
Ok(NodeOutput::SoftFail)
}
}
impl<Ctx: Send> Node<(), NodeOutput<i32>, (), Ctx> for B {
async fn run(&mut self, _: (), _: &mut Ctx) -> Result<NodeOutput<i32>, ()> {
Ok(NodeOutput::Ok(5))
}
}
async fn main() {
let mut flow = ParallelFlow::<(), i32, (), _>::builder()
.add_node(A)
.add_node(B)
.build(async |_input, context: &mut ExampleCtx| {
Ok(NodeOutput::Ok(120))
});
let mut ctx = ExampleCtx;
let result = flow.run((), &mut ctx).await;
assert_eq!(result, Ok(NodeOutput::Ok(120)));
}Implementations§
Source§impl<Input, Output, Error, Context> ParallelFlow<Input, Output, Error, Context>
impl<Input, Output, Error, Context> ParallelFlow<Input, Output, Error, Context>
Sourcepub fn builder() -> Builder<Input, Output, Error, Context>
pub fn builder() -> Builder<Input, Output, Error, Context>
Creates a new Builder for constructing ParallelFlow.
See also ParallelFlow.
§Examples
use node_flow::flows::ParallelFlow;
let builder = ParallelFlow::<u8, u16, (), Ctx>::builder();Trait Implementations§
Source§impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Clone for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>where
J: Clone,
impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Clone for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>where
J: Clone,
Source§impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Debug for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>where
NodeTypes: ChainDebug,
impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Debug for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>where
NodeTypes: ChainDebug,
Source§impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Node<Input, NodeOutput<Output>, Error, Context> for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes> Node<Input, NodeOutput<Output>, Error, Context> for ParallelFlow<Input, Output, Error, Context, ChainRunOutput, J, NodeTypes, NodeIOETypes>
Source§fn run(
&mut self,
input: Input,
context: &mut Context,
) -> impl Future<Output = Result<NodeOutput<Output>, Error>> + Send
fn run( &mut self, input: Input, context: &mut Context, ) -> impl Future<Output = Result<NodeOutput<Output>, Error>> + Send
Runs the node. Read more
Source§fn describe(&self) -> Description
fn describe(&self) -> Description
Describes this node, its type signature and other specifics. Read more
Auto Trait Implementations§
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> Freeze for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>where
Joiner: Freeze,
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> RefUnwindSafe for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>where
Joiner: RefUnwindSafe,
NodeTypes: RefUnwindSafe,
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> Send for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> Sync for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> Unpin for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>where
Joiner: Unpin,
impl<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes> UnwindSafe for ParallelFlow<Input, Output, Error, Context, ChainOutput, Joiner, NodeTypes, NodeIOETypes>where
Joiner: UnwindSafe,
NodeTypes: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<Input, Output, Error, Context, T> BoxedNode<Input, Output, Error, Context> for Twhere
T: Node<Input, Output, Error, Context>,
impl<Input, Output, Error, Context, T> BoxedNode<Input, Output, Error, Context> for Twhere
T: Node<Input, Output, Error, Context>,
Source§fn run_boxed<'life0, 'life1, 'async_trait>(
&'life0 mut self,
input: Input,
context: &'life1 mut Context,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Input: 'async_trait,
Output: 'async_trait,
Error: 'async_trait,
T: 'async_trait,
fn run_boxed<'life0, 'life1, 'async_trait>(
&'life0 mut self,
input: Input,
context: &'life1 mut Context,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Input: 'async_trait,
Output: 'async_trait,
Error: 'async_trait,
T: 'async_trait,
Available on crate feature
boxed_node only.Runs the node. Read more
Source§fn describe(&self) -> Description
fn describe(&self) -> Description
Available on crate feature
boxed_node only.Describes this node, its type signature and other specifics. Read more