Skip to main content

CoreTask

Trait CoreTask 

Source
pub trait CoreTask: Send + Sync {
    type Input;
    type Output;
    type Future: Future<Output = Result<Self::Output, BoxError>> + Send;

    // Required method
    fn run(&self, input: Self::Input) -> Self::Future;
}
Expand description

A core task is a task that can be run by the workflow runtime.

Tasks can be defined either as closures (via WorkflowBuilder::then) or as structs implementing this trait directly. Struct-based tasks are useful for:

  • Reusable task logic across workflows
  • Tasks with configuration/state
  • Serializable workflows (tasks can be registered by ID)

§Example

use sayiir_core::prelude::*;
use std::pin::Pin;
use std::future::Future;

/// A task that doubles its input.
struct DoubleTask;

impl CoreTask for DoubleTask {
    type Input = u32;
    type Output = u32;
    type Future = Pin<Box<dyn Future<Output = Result<u32, BoxError>> + Send>>;

    fn run(&self, input: u32) -> Self::Future {
        Box::pin(async move { Ok(input * 2) })
    }
}

/// A configurable task with state.
struct MultiplyTask {
    factor: u32,
}

impl CoreTask for MultiplyTask {
    type Input = u32;
    type Output = u32;
    type Future = Pin<Box<dyn Future<Output = Result<u32, BoxError>> + Send>>;

    fn run(&self, input: u32) -> Self::Future {
        let factor = self.factor;
        Box::pin(async move { Ok(input * factor) })
    }
}

Required Associated Types§

Required Methods§

Source

fn run(&self, input: Self::Input) -> Self::Future

Run the task with the given input and return the output.

Implementors§

Source§

impl<F, I, O, Fut> CoreTask for FnTask<F, I, O, Fut>
where F: Fn(I) -> Fut + Send + Sync, I: Send, O: Send, Fut: Future<Output = Result<O, BoxError>> + Send,

Source§

type Input = I

Source§

type Output = O

Source§

type Future = Fut