use core::marker::PhantomData;
use crate::codec_prelude::*;
pub fn decoder<C, T>(consumer: C) -> Decoder<C, T> {
Decoder::new(consumer)
}
pub struct Decoder<P, T> {
inner: P,
phantom: PhantomData<T>,
}
impl<P, T> Decoder<P, T> {
fn new(producer: P) -> Self {
Self {
inner: producer,
phantom: PhantomData,
}
}
pub fn into_inner(self) -> P {
self.inner
}
}
impl<P, T, Symbol> Producer for Decoder<P, T>
where
P: BulkProducer<Item = Symbol>,
T: Decodable<Symbol>,
{
type Item = T;
type Final = P::Final;
type Error = DecodeError<P::Final, P::Error, T::ErrorReason>;
async fn produce(&mut self) -> Result<Either<Self::Item, Self::Final>, Self::Error> {
if let Right(fin) = self.inner.expose_items_sync(|_| (0, ())).await? {
return Ok(Right(fin));
}
Ok(Left(T::decode(&mut self.inner).await?))
}
async fn slurp(&mut self) -> Result<(), Self::Error> {
Ok(self.inner.slurp().await?)
}
}