pub struct FnFlow<Input, Output, Error, Context, InnerData = (), R = ()> { /* private fields */ }Expand description
FnFlow takes some async function and wraps around it to crate a node.
This flow allows for setting custom Description
through the FnFlow::with_description function,
since there is no other way to get it (like with Node::describe).
§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.
§Examples
use node_flow::flows::FnFlow;
use node_flow::node::{Node, NodeOutput};
#[derive(Clone)]
struct SomeExpensiveData(Vec<u32>);
async fn main() {
let mut flow = FnFlow::<u32, u32, (), _>::new(
SomeExpensiveData((0..1<<15).collect()),
async |SomeExpensiveData(data), input, _: &mut _| {
let res = data.iter().sum::<u32>() / data.len() as u32 + input;
Ok(NodeOutput::Ok(res))
},
);
let result = flow.run(1, &mut ()).await;
assert_eq!(result, Ok(NodeOutput::Ok(1<<14)));
}Implementations§
Source§impl<Input, Output, Error, Context> FnFlow<Input, Output, Error, Context>
impl<Input, Output, Error, Context> FnFlow<Input, Output, Error, Context>
Sourcepub fn new<InnerData, R>(
inner_data: InnerData,
runner: R,
) -> FnFlow<Input, Output, Error, Context, InnerData, R>
pub fn new<InnerData, R>( inner_data: InnerData, runner: R, ) -> FnFlow<Input, Output, Error, Context, InnerData, R>
Creates a new FnFlow from a given runner.
The runner must satisfy:
Self:Runner<'_, Input, Output, Error, _, InnerData>
When using closure as a runner it always needs:
- to be an async closure - because of lifetimes
- for context to:
- have the type of a context specified when using context - because it cannot infer the type
or - have the context specified as
_: &mut _when not using context - because it cannot satisfy thatRunneris implemented
- have the type of a context specified when using context - because it cannot infer the type
- to have the type of inner data specified when using inner data - because it cannot infer the type
§Examples
struct SomeData(u16);
FnFlow::<u8, u16, (), Context>::new(
SomeData(15),
async |_, _, _: &mut _| {
Ok(NodeOutput::Ok(30))
},
);
FnFlow::<u8, u16, (), Context>::new(
SomeData(15),
async |data: SomeData, _, _: &mut _| {
Ok(NodeOutput::Ok(data.0 + 30))
},
);
FnFlow::<u8, u16, (), Context>::new(
SomeData(15),
async |_, _, ctx: &mut Context| {
let _forked_ctx = ctx.fork();
Ok(NodeOutput::Ok(30))
},
);Source§impl<Input, Output, Error, Context, InnerData, R> FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> FnFlow<Input, Output, Error, Context, InnerData, R>
Sourcepub fn with_description(self, description: Description) -> Self
pub fn with_description(self, description: Description) -> Self
Attaches a custom Description to this flow.
This is be useful when something complex happens in the flow.
§Examples
use node_flow::flows::FnFlow;
use node_flow::describe::{Description, DescriptionBase};
use node_flow::node::NodeOutput;
let desc = Description::Node {
base: DescriptionBase::from::<(), String, usize, u8, u32>()
}.with_description("I am lying about my types! But shh...");
let flow = FnFlow::<u8, u16, (), ()>::new((), async|_, x, _: &mut _| {
Ok(NodeOutput::Ok(x as u16))
}).with_description(desc);Trait Implementations§
Source§impl<Input, Output, Error, Context, InnerData, R> Clone for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> Clone for FnFlow<Input, Output, Error, Context, InnerData, R>
Source§impl<Input, Output, Error, Context, InnerData, R> Debug for FnFlow<Input, Output, Error, Context, InnerData, R>where
InnerData: Debug,
impl<Input, Output, Error, Context, InnerData, R> Debug for FnFlow<Input, Output, Error, Context, InnerData, R>where
InnerData: Debug,
Source§impl<Input, Output, Error, Context, InnerData, R> Node<Input, NodeOutput<Output>, Error, Context> for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> Node<Input, NodeOutput<Output>, Error, Context> for FnFlow<Input, Output, Error, Context, InnerData, R>
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, InnerData, R> Freeze for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> RefUnwindSafe for FnFlow<Input, Output, Error, Context, InnerData, R>where
InnerData: RefUnwindSafe,
R: RefUnwindSafe,
impl<Input, Output, Error, Context, InnerData, R> Send for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> Sync for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> Unpin for FnFlow<Input, Output, Error, Context, InnerData, R>
impl<Input, Output, Error, Context, InnerData, R> UnsafeUnpin for FnFlow<Input, Output, Error, Context, InnerData, R>where
InnerData: UnsafeUnpin,
R: UnsafeUnpin,
impl<Input, Output, Error, Context, InnerData, R> UnwindSafe for FnFlow<Input, Output, Error, Context, InnerData, R>where
InnerData: UnwindSafe,
R: UnwindSafe,
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