summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-03-01 18:03:04 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-03-01 19:11:19 +0100
commiteb675b44bc62fbe2984c8a2831b14a7feb7e2d18 (patch)
tree28a44490bdc1baba59255a083ebf43bb5cc63b7e
parent7ba3b2f2b0a8503a528c4f37ce932314e6fb44d6 (diff)
Add iterator extension for reporting from iterator
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/core/libimagrt/src/runtime.rs56
1 files changed, 56 insertions, 0 deletions
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<E, F>
+ where Self: Iterator<Item = E> + Sized,
+ E: Sized,
+ F: Fn(&E) -> Option<StoreId>
+{
+ fn report_entries_touched<'a>(self, rt: &'a Runtime<'a>, func: F) -> TouchIterator<'a, Self, E, F> {
+ TouchIterator {
+ inner: self,
+ rt,
+ func
+ }
+ }
+}
+
+impl<I, E, F> IntoTouchIterator<E, F> for I
+ where I: Iterator<Item = E>,
+ E: Sized,
+ F: Fn(&E) -> Option<StoreId>
+{
+ // default implementation
+}
+
+pub struct TouchIterator<'a, I, E, F>
+ where I: Iterator<Item = E>,
+ E: Sized,
+ F: Fn(&E) -> Option<StoreId>
+{
+ inner: I,
+ rt: &'a Runtime<'a>,
+ func: F
+}
+
+impl<'a, I, E, F> Iterator for TouchIterator<'a, I, E, F>
+ where I: Iterator<Item = E>,
+ E: Sized,
+ F: Fn(&E) -> Option<StoreId>
+{
+ type Item = Result<E>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ 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