diff --git a/lib/entry/libimagentryview/src/viewer.rs b/lib/entry/libimagentryview/src/viewer.rs index 9c11d7d4..785947b0 100644 --- a/lib/entry/libimagentryview/src/viewer.rs +++ b/lib/entry/libimagentryview/src/viewer.rs @@ -40,3 +40,39 @@ pub trait Viewer { Ok(()) } } + +/// Extension for all iterators, so that an iterator can be viewed with: +/// +/// ```ignore +/// iter.view::(&mut sink) +/// ``` +/// +pub trait ViewFromIter { + fn view(self, sink: &mut W) -> Result<()> + where V: Viewer + Default, + W: Write; + + fn view_with(self, v: V, sink: &mut W) -> Result<()> + where V: Viewer, + W: Write; +} + +impl ViewFromIter for I + where I: Iterator, + E: Deref +{ + fn view(self, sink: &mut W) -> Result<()> + where V: Viewer + Default, + W: Write + { + self.view_with(V::default(), sink) + } + + fn view_with(self, v: V, sink: &mut W) -> Result<()> + where V: Viewer, + W: Write + { + v.view_entries(self, sink) + } +} +