From b4f401675e159e9173109390ff3409d338635548 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 5 Jan 2019 01:04:21 +0100 Subject: [PATCH] Add Entries::into_storeid_iter() This is a more complicated functionality (implementation-wise) of the `Entries` iterator, which allows a callee to transform it into a `StoreIdIterator`. It uses the crate internal `PathIterator` type and a function to extract the internals of that iterator which is then turned into an iterator over `Result`, which can be used to build a `StoreIdIterator`. The implementation is ugly, but we need to be able to transform a `Entries` iterator into a `StoreIdIterator` in the `libimagentryannotation`, and possibly in more cases where we want to be agnostic over iterators. Maybe there is a cleaner solution to this, hence the comment about whether this should be removed. Signed-off-by: Matthias Beyer --- .../libimagstore/src/file_abstraction/iter.rs | 12 ++++++++++++ lib/core/libimagstore/src/iter.rs | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/core/libimagstore/src/file_abstraction/iter.rs b/lib/core/libimagstore/src/file_abstraction/iter.rs index 5ef5d386..2eda4c27 100644 --- a/lib/core/libimagstore/src/file_abstraction/iter.rs +++ b/lib/core/libimagstore/src/file_abstraction/iter.rs @@ -71,6 +71,18 @@ impl<'a> PathIterator<'a> { self } + /// Turn iterator into its internals + /// + /// Used for `Entries::into_storeid_iter()` + /// + /// # TODO + /// + /// Revisit whether this can be done in a cleaner way. See commit message for why this is + /// needed. + pub(crate) fn into_inner(self) -> Box>> { + self.iter + } + } impl<'a> Iterator for PathIterator<'a> { diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs index e236d984..9ab6ed36 100644 --- a/lib/core/libimagstore/src/iter.rs +++ b/lib/core/libimagstore/src/iter.rs @@ -138,6 +138,7 @@ mod compile_test { } use storeid::StoreId; +use storeid::StoreIdIterator; use self::delete::StoreDeleteIterator; use self::get::StoreGetIterator; use self::retrieve::StoreRetrieveIterator; @@ -178,10 +179,18 @@ impl<'a> Entries<'a> { Entries(self.0.in_collection(c), self.1) } - // TODO: Remove or fix me - //pub fn without_store(self) -> StoreIdIterator { - // StoreIdIterator::new(Box::new(self.0.map(|r| r.map(|id| id.without_base())))) - //} + /// Turn `Entries` iterator into generic `StoreIdIterator` + /// + /// # TODO + /// + /// Revisit whether this can be done in a cleaner way. See commit message for why this is + /// needed. + pub fn into_storeid_iter(self) -> StoreIdIterator { + let iter = self.0 + .into_inner() + .map(|r| r.and_then(StoreId::new)); + StoreIdIterator::new(Box::new(iter)) + } /// Transform the iterator into a StoreDeleteIterator ///