use crate::action::Action;
use crate::error::CompressionError;
pub trait EncodeExt<I>
where
I: Iterator,
{
fn encode<E: Encoder<In = I::Item>>(
self,
encoder: &mut E,
action: Action,
) -> EncodeIterator<'_, I, E>
where
CompressionError: From<E::Error>;
}
impl<I> EncodeExt<I::IntoIter> for I
where
I: IntoIterator,
{
fn encode<E: Encoder<In = I::Item>>(
self,
encoder: &mut E,
action: Action,
) -> EncodeIterator<'_, I::IntoIter, E>
where
CompressionError: From<E::Error>,
{
EncodeIterator::<I::IntoIter, E>::new(self.into_iter(), encoder, action)
}
}
#[derive(Debug)]
pub struct EncodeIterator<'a, I, E>
where
I: Iterator<Item = E::In>,
E: Encoder,
CompressionError: From<E::Error>,
{
encoder: &'a mut E,
action: Action,
inner: I,
}
impl<'a, I, E> EncodeIterator<'a, I, E>
where
I: Iterator<Item = E::In>,
E: Encoder,
CompressionError: From<E::Error>,
{
fn new(inner: I, encoder: &'a mut E, action: Action) -> Self {
Self {
encoder,
inner,
action,
}
}
}
impl<I, E> Iterator for EncodeIterator<'_, I, E>
where
I: Iterator<Item = E::In>,
E: Encoder,
CompressionError: From<E::Error>,
{
type Item = Result<E::Out, E::Error>;
fn next(&mut self) -> Option<Result<E::Out, E::Error>> {
self.encoder.next(&mut self.inner, self.action)
}
}
pub trait Encoder
where
CompressionError: From<Self::Error>,
{
type Error;
type In;
type Out;
fn next<I: Iterator<Item = Self::In>>(
&mut self,
iter: &mut I,
action: Action,
) -> Option<Result<Self::Out, Self::Error>>;
}