From 5972ce7f37ab630b0bf20624630bae13b5ee2beb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 12 May 2016 17:21:04 +0200 Subject: [PATCH] Make StoreIdIterator generic This patch makes the StoreIdIterator type generic over all iterators with Item = StoreId. As the StoreIdIterator type was previousely used for iterating over a glob() result, I had to wrap this result type in another iterator type which is then wrapped in StoreIdIterator. With this patch applied, other libraries may use the StoreIdIterator type to abstract things away in their implementation. --- libimagstore/src/store.rs | 4 ++-- libimagstore/src/storeid.rs | 39 +++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index c4d08326..db9eac3f 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -25,7 +25,7 @@ use walkdir::Iter as WalkDirIter; use error::{ParserErrorKind, ParserError}; use error::{StoreError, StoreErrorKind}; -use storeid::{IntoStoreId, StoreId, StoreIdIterator}; +use storeid::{IntoStoreId, StoreId, GlobStoreIdIterator, StoreIdIterator}; use lazyfile::LazyFile; use hook::aspect::Aspect; @@ -361,7 +361,7 @@ impl Store { let path = [ path, "/*" ].join(""); debug!("glob()ing with '{}'", path); glob(&path[..]) - .map(StoreIdIterator::new) + .map(|paths| StoreIdIterator::new(Box::new(GlobStoreIdIterator::new(paths)))) .map_err(|e| StoreError::new(StoreErrorKind::GlobError, Some(Box::new(e)))) } else { Err(StoreError::new(StoreErrorKind::EncodingError, None)) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index d5229e2b..cdcf1676 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -155,10 +155,41 @@ macro_rules! module_entry_path_mod { ) } -pub struct StoreIdIterator { +pub struct GlobStoreIdIterator { paths: Paths, } +impl Debug for GlobStoreIdIterator { + + fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> { + write!(fmt, "GlobStoreIdIterator") + } + +} + +impl GlobStoreIdIterator { + + pub fn new(paths: Paths) -> GlobStoreIdIterator { + GlobStoreIdIterator { + paths: paths, + } + } + +} + +impl Iterator for GlobStoreIdIterator { + type Item = StoreId; + + fn next(&mut self) -> Option { + self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p)) + } + +} + +pub struct StoreIdIterator { + iter: Box>, +} + impl Debug for StoreIdIterator { fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> { @@ -169,9 +200,9 @@ impl Debug for StoreIdIterator { impl StoreIdIterator { - pub fn new(paths: Paths) -> StoreIdIterator { + pub fn new(iter: Box>) -> StoreIdIterator { StoreIdIterator { - paths: paths, + iter: iter, } } @@ -181,7 +212,7 @@ impl Iterator for StoreIdIterator { type Item = StoreId; fn next(&mut self) -> Option { - self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p)) + self.iter.next() } }