summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-09-03 10:55:38 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-10-04 17:50:30 +0200
commit7fc3961a17b1ae63e0d7758d00252e9f38d8cadd (patch)
tree70b4a1dac836f584e2086ef524cb65f33b1a92e0
parentd8df96ad1fa99940a07a86cebccaab61b6d1bff1 (diff)
downloadimag-7fc3961a17b1ae63e0d7758d00252e9f38d8cadd.zip
imag-7fc3961a17b1ae63e0d7758d00252e9f38d8cadd.tar.gz
Add viewer helper trait for iterators
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/entry/libimagentryview/src/viewer.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/entry/libimagentryview/src/viewer.rs b/lib/entry/libimagentryview/src/viewer.rs
index 9c11d7d..785947b 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::<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)
+ }
+}
+