From eb675b44bc62fbe2984c8a2831b14a7feb7e2d18 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 18:03:04 +0100 Subject: Add iterator extension for reporting from iterator Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/runtime.rs | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 4522a1f0..c29a2846 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -577,6 +577,62 @@ impl<'a> Runtime<'a> { } } +pub trait IntoTouchIterator + where Self: Iterator + Sized, + E: Sized, + F: Fn(&E) -> Option +{ + fn report_entries_touched<'a>(self, rt: &'a Runtime<'a>, func: F) -> TouchIterator<'a, Self, E, F> { + TouchIterator { + inner: self, + rt, + func + } + } +} + +impl IntoTouchIterator for I + where I: Iterator, + E: Sized, + F: Fn(&E) -> Option +{ + // default implementation +} + +pub struct TouchIterator<'a, I, E, F> + where I: Iterator, + E: Sized, + F: Fn(&E) -> Option +{ + inner: I, + rt: &'a Runtime<'a>, + func: F +} + +impl<'a, I, E, F> Iterator for TouchIterator<'a, I, E, F> + where I: Iterator, + E: Sized, + F: Fn(&E) -> Option +{ + type Item = Result; + + fn next(&mut self) -> Option { + while let Some(next) = self.inner.next() { + match (self.func)(&next) { + Some(id) => if let Err(e) = self.rt.report_touched(&id) { + return Some(Err(e)) + } else { + return Some(Ok(next)) + }, + + None => continue, + } + } + + None + } +} + /// A trait for providing ids from clap argument matches /// /// This trait can be implement on a type so that it can provide IDs when given a ArgMatches -- cgit v1.2.3