~matthiasbeyer/imag

7fc3961a17b1ae63e0d7758d00252e9f38d8cadd — Matthias Beyer 2 years ago d8df96a
Add viewer helper trait for iterators

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 files changed, 36 insertions(+), 0 deletions(-)

M lib/entry/libimagentryview/src/viewer.rs
M lib/entry/libimagentryview/src/viewer.rs => lib/entry/libimagentryview/src/viewer.rs +36 -0
@@ 40,3 40,39 @@ pub trait Viewer {
        Ok(())
    }
}

/// Extension for all iterators, so that an iterator can be viewed with:
///
/// ```ignore
///     iter.view::<Viewer, _>(&mut sink)
/// ```
///
pub trait ViewFromIter {
    fn view<V, W>(self, sink: &mut W) -> Result<()>
        where V: Viewer + Default,
              W: Write;

    fn view_with<V, W>(self, v: V, sink: &mut W) -> Result<()>
        where V: Viewer,
              W: Write;
}

impl<I, E> ViewFromIter for I
    where I: Iterator<Item = E>,
          E: Deref<Target = Entry>
{
    fn view<V, W>(self, sink: &mut W) -> Result<()>
        where V: Viewer + Default,
              W: Write
    {
        self.view_with(V::default(), sink)
    }

    fn view_with<V, W>(self, v: V, sink: &mut W) -> Result<()>
        where V: Viewer,
              W: Write
    {
        v.view_entries(self, sink)
    }
}