dewit 0.0.1

Define scheduling and execution of code separately from data flow
Documentation
//! The way in which a task is executed.

use crate::task::{Task, TaskOutput};

pub mod runtime;

/// An abstract execution mode. Represents various ways in which code executes,
/// such as in a single thread, on a thread pool, or on an async runtime.
pub trait Mode<'src> {
    /// The output of this mode for a type.
    type Output<O>: Send
    where
        O: Send + 'src;

    /// Execute the given task with this mode.
    fn invoke<I, O, T>(&'src self, task: T, input: I) -> TaskOutput<'src, Self, O>
    where
        T: Task<'src, I, O>,
        O: Send + 'src;

    /// Bind the result of the closure into an output, preferring to execute the
    /// closure on a blocking thread pool if available.
    fn spawn_blocking<O>(&self, f: impl FnOnce() -> O + Send + 'src) -> Self::Output<O>
    where
        O: Send + 'src;
    /// Bind the result of the closure into an output, preferring to execute the
    /// closure on a work-stealing thread pool if available.
    fn spawn_cpu<O>(&self, f: impl FnOnce() -> O + Send + 'src) -> Self::Output<O>
    where
        O: Send + 'src;
    /// Bind the result of the future into an output, preferring to execute the
    /// future on an async runtime if the Mode provides one.
    fn spawn_io<O, Fut>(&self, f: impl FnOnce() -> Fut + Send + 'src) -> Self::Output<O>
    where
        O: Send + 'src,
        Fut: Future<Output = O> + Send + 'src;

    /// Dispatch multiple [`Output`](Self::Output), potentially in parallel.
    fn join<AO, BO>(
        &'src self,
        a: impl FnOnce(&'src Self) -> Self::Output<AO> + Send + 'src,
        b: impl FnOnce(&'src Self) -> Self::Output<BO> + Send + 'src,
    ) -> Self::Output<(AO, BO)>
    where
        AO: Send + 'src,
        BO: Send + 'src;

    /// Given an [`Output`](Self::Output), apply the closure and generate a new
    /// output.
    fn then<I, O>(
        &'src self,
        input: Self::Output<I>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
    ) -> Self::Output<O>
    where
        I: Send + 'src,
        O: Send + 'src;

    /// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
    /// closure only if the Result is [`Ok`] and generate a new output.
    fn map_result<I, E, O>(
        &'src self,
        input: Self::Output<Result<I, E>>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
    ) -> Self::Output<Result<O, E>>
    where
        I: Send + 'src,
        E: Send + 'src,
        O: Send + 'src;
    /// Given an [`Output`](Self::Output) wrapped in an [`Option`], apply the
    /// closure only if the Option is [`Some`] and generate a new output.
    fn map_option<I, O>(
        &'src self,
        input: Self::Output<Option<I>>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
    ) -> Self::Output<Option<O>>
    where
        I: Send + 'src,
        O: Send + 'src;
    /// TODO doc iter
    fn map_iterator<I, O, II>(
        &'src self,
        input: Self::Output<II>,
        f: impl Fn(&'src Self, I) -> Self::Output<O> + Send + 'src,
    ) -> Self::Output<impl Iterator<Item = O> + Send + 'src>
    where
        O: Send + 'src,
        II: Iterator<Item = I> + Send + 'src;

    /// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
    /// closure only if the Result is [`Ok`] and generate a new [`Result`]
    /// output.
    fn and_then_result<I, E, O>(
        &'src self,
        input: Self::Output<Result<I, E>>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<Result<O, E>> + Send + 'src,
    ) -> Self::Output<Result<O, E>>
    where
        I: Send + 'src,
        E: Send + 'src,
        O: Send + 'src;
    /// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
    /// closure only if the Result is [`Ok`] and generate a new [`Result`]
    /// output.
    fn and_then_option<I, O>(
        &'src self,
        input: Self::Output<Option<I>>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<Option<O>> + Send + 'src,
    ) -> Self::Output<Option<O>>
    where
        I: Send + 'src,
        O: Send + 'src;
}