From 7fc3961a17b1ae63e0d7758d00252e9f38d8cadd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 3 Sep 2019 10:55:38 +0200 Subject: [PATCH] Add viewer helper trait for iterators Signed-off-by: Matthias Beyer --- lib/entry/libimagentryview/src/viewer.rs | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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) + } +} +