result-transformer-flow 0.0.1

Traits, macros and utilities for transforming Result values.
Documentation
//! Flow utilities for transforming an entire [`Result`] asynchronously.
//!
//! These APIs are implemented with `async fn` and keep overhead low. For the
//! best performance, prefer implementing `AsyncResultTransformer` directly
//! instead of chaining flows.

use std::marker::PhantomData;

/// A transformation working on `Result` values in an asynchronous context.
pub trait AsyncResultFlow<InputOk, InputErr> {
    /// Success type produced by the flow.
    type OutputOk;
    /// Error type produced by the flow.
    type OutputErr;

    /// Apply the transformation to the given [`Result`].
    fn apply_result_async<'a>(
        &'a self,
        input: Result<InputOk, InputErr>,
    ) -> impl Future<Output = Result<Self::OutputOk, Self::OutputErr>> + Send + 'a;

    /// Chain another [`AsyncResultFlow`] after this one.
    fn then_async_result<Next>(
        self,
        next: Next,
    ) -> AsyncResultFlowChain<Self, Next, InputOk, InputErr>
    where
        Self: Sized,
        Next: AsyncResultFlow<Self::OutputOk, Self::OutputErr>,
    {
        AsyncResultFlowChain {
            head: self,
            next,
            _phantom: PhantomData,
        }
    }
}

/// Composition of two [`AsyncResultFlow`] implementations.
pub struct AsyncResultFlowChain<Head, Next, InputOk, InputErr>
where
    Head: AsyncResultFlow<InputOk, InputErr>,
    Next: AsyncResultFlow<Head::OutputOk, Head::OutputErr>,
{
    head: Head,
    next: Next,
    _phantom: PhantomData<(InputOk, InputErr)>,
}

impl<Head, Next, InputOk, InputErr> AsyncResultFlow<InputOk, InputErr>
    for AsyncResultFlowChain<Head, Next, InputOk, InputErr>
where
    Head: AsyncResultFlow<InputOk, InputErr> + Send + Sync,
    Next: AsyncResultFlow<Head::OutputOk, Head::OutputErr> + Send + Sync,
    InputOk: Send + Sync,
    InputErr: Send + Sync,
{
    type OutputOk = Next::OutputOk;
    type OutputErr = Next::OutputErr;

    fn apply_result_async<'a>(
        &'a self,
        input_result: Result<InputOk, InputErr>,
    ) -> impl Future<Output = Result<Self::OutputOk, Self::OutputErr>> + Send + 'a {
        async {
            let intermediate = self.head.apply_result_async(input_result).await;
            self.next.apply_result_async(intermediate).await
        }
    }
}

// `Clone` implementation when both flows are cloneable
impl<Head, Next, InputOk, InputErr> Clone for AsyncResultFlowChain<Head, Next, InputOk, InputErr>
where
    Head: AsyncResultFlow<InputOk, InputErr> + Clone,
    Next: AsyncResultFlow<Head::OutputOk, Head::OutputErr> + Clone,
{
    fn clone(&self) -> Self {
        Self {
            head: self.head.clone(),
            next: self.next.clone(),
            _phantom: PhantomData,
        }
    }
}

// Optional `Copy` implementation when both flows are copyable
impl<Head, Next, InputOk, InputErr> Copy for AsyncResultFlowChain<Head, Next, InputOk, InputErr>
where
    Head: AsyncResultFlow<InputOk, InputErr> + Copy,
    Next: AsyncResultFlow<Head::OutputOk, Head::OutputErr> + Copy,
{
}