Provide StoreId Iterator which has a ref to the Store

This change is needed so we can refactor the "get" iterator to not take
an argument (the store) later, which improves the API.
This commit is contained in:
Matthias Beyer 2018-01-28 21:10:12 +01:00
parent da4b823048
commit 030e32e44f
3 changed files with 32 additions and 5 deletions

View file

@ -45,7 +45,6 @@ use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::MapErrTrace;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::error::StoreError as Error;
use libimagentrylink::internal::*;

View file

@ -40,7 +40,7 @@ use toml_query::read::TomlValueReadTypeExt;
use error::{StoreError as SE, StoreErrorKind as SEK};
use error::ResultExt;
use storeid::{IntoStoreId, StoreId, StoreIdIterator};
use storeid::{IntoStoreId, StoreId, StoreIdIterator, StoreIdIteratorWithStore};
use file_abstraction::FileAbstractionInstance;
// We re-export the following things so tests can use them
@ -721,7 +721,7 @@ impl Store {
}
/// Get _all_ entries in the store (by id as iterator)
pub fn entries(&self) -> Result<StoreIdIterator> {
pub fn entries<'a>(&'a self) -> Result<StoreIdIteratorWithStore<'a>> {
self.backend
.pathes_recursively(self.path().clone())
.and_then(|iter| {
@ -738,9 +738,8 @@ impl Store {
elems.push(sid);
}
}
Ok(StoreIdIterator::new(Box::new(elems.into_iter())))
Ok(StoreIdIteratorWithStore::new(Box::new(elems.into_iter()), self))
})
}
/// Gets the path where this store is on the disk

View file

@ -30,6 +30,7 @@ use error::StoreErrorKind as SEK;
use error::StoreError as SE;
use error::ResultExt;
use store::Result;
use store::Store;
/// The Index into the Store
#[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)]
@ -260,6 +261,34 @@ impl Iterator for StoreIdIterator {
}
pub struct StoreIdIteratorWithStore<'a>(StoreIdIterator, &'a Store);
impl<'a> Deref for StoreIdIteratorWithStore<'a> {
type Target = StoreIdIterator;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> Iterator for StoreIdIteratorWithStore<'a> {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
self.0.next()
}
}
impl<'a> StoreIdIteratorWithStore<'a> {
pub fn new(iter: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
StoreIdIteratorWithStore(StoreIdIterator::new(iter), store)
}
pub fn get_store(&self) -> &Store {
&self.1
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;