Add viewer helper trait for iterators

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-09-03 10:55:38 +02:00
parent d8df96ad1f
commit 7fc3961a17

View file

@ -40,3 +40,39 @@ pub trait Viewer {
Ok(()) 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)
}
}