use core::convert::Infallible;
use core::fmt::Debug;
use either::Either;
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct ErrorImmediately<Err>(Option<Err>);
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"),
))
}
}