ufotofu 0.10.1

Abstractions for lazily consuming and producing sequences
Documentation
use core::convert::Infallible;
use core::fmt::Debug;

use either::Either;

use crate::prelude::*;

/// A (bulk) producer that immediately yields a predetermined error.
///
/// <br/>Counterpart: the [`consumer::ErrorImmediately`] type.
#[derive(Debug, Clone, Copy)]

pub struct ErrorImmediately<Err>(Option<Err>);

/// Creates a producer that returns an error and nothing else.
///
/// ```
/// # use ufotofu::prelude::*;
/// use producer::error_immediately;
/// # pollster::block_on(async {
///
/// let mut p = error_immediately(17);
/// assert_eq!(p.produce().await, Err(17));
/// # });
/// ```
///
/// <br/>Counterpart: the [consumer::error_immediately] function.
pub fn error_immediately<Err>(err: Err) -> ErrorImmediately<Err> {
    ErrorImmediately(Some(err))
}

impl<Err> Producer for ErrorImmediately<Err> {
    type Item = Infallible;
    type Final = Infallible;
    type Error = Err;

    async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
        Err(self
            .0
            .take()
            .expect("Must not call produce after having yielded an error"))
    }

    async fn slurp(&mut self) -> Result<(), Self::Error> {
        Err(self
            .0
            .take()
            .expect("Must not call slurp after having yielded an error"))
    }
}

impl<Err> BulkProducer for ErrorImmediately<Err> {
    async fn expose_items_gracefully<F, R>(
        &mut self,
        f: F,
    ) -> Result<Either<R, (F, Self::Final)>, (F, Self::Error)>
    where
        F: AsyncFnOnce(&[Self::Item]) -> (usize, R),
    {
        Err((
            f,
            self.0
                .take()
                .expect("Must not call an expose_items method after having yielded an error"),
        ))
    }
}