dewit 0.0.1

Define scheduling and execution of code separately from data flow
Documentation
use crate::{
    mode::Mode,
    task::{
        Task,
        TaskOutput,
    },
};

/// Executes all code as soon as possible on a single thread.
pub struct Blocking;

impl<'src> Mode<'src> for Blocking {
    type Output<O>
        = O
    where
        O: Send + 'src;

    #[inline(always)]
    fn invoke<I, O, T>(&'src self, task: T, input: I) -> TaskOutput<'src, Self, O>
    where
        T: Task<'src, I, O>,
        O: Send + 'src,
    {
        task.go(self, input)
    }

    #[inline(always)]
    fn spawn_blocking<O>(&self, f: impl FnOnce() -> O) -> Self::Output<O>
    where
        O: Send + 'src,
    {
        f()
    }
    #[inline(always)]
    fn spawn_cpu<O>(&self, f: impl FnOnce() -> O) -> Self::Output<O>
    where
        O: Send + 'src,
    {
        f()
    }
    #[inline(always)]
    fn spawn_io<O, Fut>(&self, f: impl FnOnce() -> Fut) -> Self::Output<O>
    where
        O: Send + 'src,
        Fut: Future<Output = O>,
    {
        pollster::block_on(f())
    }

    #[inline(always)]
    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,
    {
        (a(self), b(self))
    }

    #[inline(always)]
    fn then<I, O>(
        &'src self,
        input: Self::Output<I>,
        f: impl FnOnce(&'src Self, I) -> Self::Output<O>,
    ) -> Self::Output<O>
    where
        I: Send + 'src,
        O: Send + 'src,
    {
        f(self, input)
    }

    #[inline(always)]
    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,
    {
        input.map(move |input| f(self, input))
    }
    #[inline(always)]
    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,
    {
        input.map(move |input| f(self, input))
    }
    #[inline(always)]
    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,
    {
        input.map(move |input| f(self, input))
    }

    #[inline(always)]
    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,
    {
        input.and_then(move |input| f(self, input))
    }
    #[inline(always)]
    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,
    {
        input.and_then(move |input| f(self, input))
    }
}