use core::convert::Infallible;
use core::fmt::Debug;
use either::Either;
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct Empty<T>(Option<T>);
pub fn empty<T>(fin: T) -> Empty<T> {
Empty(Some(fin))
}
impl<T> Producer for Empty<T> {
type Item = Infallible;
type Final = T;
type Error = Infallible;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
Ok(Right(self.0.take().expect(
"Must not call produce after having yielded the final value",
)))
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
impl<T> BulkProducer for Empty<T> {
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),
{
Ok(Right((
f,
self.0
.take()
.expect("Must not call expose_items after having yielded the final value"),
)))
}
}