ufotofu 0.12.0

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

use crate::prelude::*;

/// A consumer that always succesfully consumes a final value, but cannot consume regular items.
///
/// <br/>Counterpart: the [`producer::Empty`] type.
#[derive(Debug, Clone, Copy)]

pub struct Full<T>(Option<T>);

/// Creates a consumer that always successfully consumes a final value, but cannot consume regular items.
///
/// ```
/// # use ufotofu::prelude::*;
/// use consumer::full;
/// # pollster::block_on(async {
///
/// let mut c = full();
/// c.consume_final(17).await?;
/// assert_eq!(c.into_final(), Some(17));
/// # Result::<(), Infallible>::Ok(())
/// # });
/// ```
///
/// <br/>Counterpart: the [producer::empty] function.
pub fn full<T>() -> Full<T> {
    Full(None)
}

impl<T> Full<T> {
    /// Returns the consumed final value, or `None` if none had been consumed yet.
    ///
    /// ```
    /// # use ufotofu::prelude::*;
    /// use consumer::full;
    /// # pollster::block_on(async {
    ///
    /// let mut c1 = full();
    /// c1.consume_final(17).await?;
    /// assert_eq!(c1.into_final(), Some(17));
    ///
    /// let mut c2 = full::<()>();
    /// assert_eq!(c2.into_final(), None);
    /// # Result::<(), Infallible>::Ok(())
    /// # });
    /// ```
    pub fn into_final(self) -> Option<T> {
        self.0
    }
}

impl<T> Consumer for Full<T> {
    type Item = Infallible;
    type Final = T;
    type Error = Infallible;

    async fn consume(&mut self, val: Either<Self::Item, Self::Final>) -> Result<(), Self::Error> {
        match val {
            Left(_) => unreachable!(),
            Right(fin) => {
                self.0 = Some(fin);
                Ok(())
            }
        }
    }

    async fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}