Minify iterator helper implementation

This commit is contained in:
Matthias Beyer 2017-09-15 13:17:12 +02:00
parent fce5df48c8
commit 8b8e4e9c6e

View file

@ -17,110 +17,79 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
pub mod create { macro_rules! mk_iterator_mod {
use storeid::StoreIdIterator; {
use store::FileLockEntry; modname = $modname:ident,
use store::Store; itername = $itername:ident,
use error::Result; iteryield = $yield:ty,
extname = $extname:ident,
extfnname = $extfnname:ident,
fun = $fun:expr
} => {
pub mod $modname {
use storeid::StoreIdIterator;
use storeid::StoreId;
#[allow(unused_imports)]
use store::FileLockEntry;
use store::Store;
use error::Result;
pub struct StoreCreateIterator<'a>(StoreIdIterator, &'a Store); pub struct $itername<'a>(StoreIdIterator, &'a Store);
impl<'a> Iterator for StoreCreateIterator<'a> { impl<'a> Iterator for $itername<'a> {
type Item = Result<FileLockEntry<'a>>; type Item = Result<$yield>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|id| self.1.create(id)) self.0.next().map(|id| $fun(id, self.1))
} }
} }
pub trait StoreIdCreateIteratorExtension<'a> { pub trait $extname<'a> {
fn into_create_iter(self, store: &'a Store) -> StoreCreateIterator<'a>; fn $extfnname(self, store: &'a Store) -> $itername<'a>;
} }
impl<'a> StoreIdCreateIteratorExtension<'a> for StoreIdIterator { impl<'a> $extname<'a> for StoreIdIterator {
fn into_create_iter(self, store: &'a Store) -> StoreCreateIterator<'a> { fn $extfnname(self, store: &'a Store) -> $itername<'a> {
StoreCreateIterator(self, store) $itername(self, store)
}
}
} }
} }
} }
pub mod delete { mk_iterator_mod! {
use storeid::StoreIdIterator; modname = create,
use store::Store; itername = StoreCreateIterator,
use error::Result; iteryield = FileLockEntry<'a>,
extname = StoreIdCreateIteratorExtension,
pub struct StoreDeleteIterator<'a>(StoreIdIterator, &'a Store); extfnname = into_create_iter,
fun = |id: StoreId, store: &'a Store| store.create(id)
impl<'a> Iterator for StoreDeleteIterator<'a> {
type Item = Result<()>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|id| self.1.delete(id))
}
}
pub trait StoreIdDeleteIteratorExtension<'a> {
fn into_delete_iter(self, store: &'a Store) -> StoreDeleteIterator<'a>;
}
impl<'a> StoreIdDeleteIteratorExtension<'a> for StoreIdIterator {
fn into_delete_iter(self, store: &'a Store) -> StoreDeleteIterator<'a> {
StoreDeleteIterator(self, store)
}
}
} }
pub mod get { mk_iterator_mod! {
use storeid::StoreIdIterator; modname = delete,
use store::FileLockEntry; itername = StoreDeleteIterator,
use store::Store; iteryield = (),
use error::Result; extname = StoreIdDeleteIteratorExtension,
extfnname = into_delete_iter,
pub struct StoreGetIterator<'a>(StoreIdIterator, &'a Store); fun = |id: StoreId, store: &'a Store| store.delete(id)
impl<'a> Iterator for StoreGetIterator<'a> {
type Item = Result<Option<FileLockEntry<'a>>>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|id| self.1.get(id))
}
}
pub trait StoreIdGetIteratorExtension<'a> {
fn into_get_iter(self, store: &'a Store) -> StoreGetIterator<'a>;
}
impl<'a> StoreIdGetIteratorExtension<'a> for StoreIdIterator {
fn into_get_iter(self, store: &'a Store) -> StoreGetIterator<'a> {
StoreGetIterator(self, store)
}
}
} }
pub mod retrieve { mk_iterator_mod! {
use storeid::StoreIdIterator; modname = get,
use store::FileLockEntry; itername = StoreGetIterator,
use store::Store; iteryield = Option<FileLockEntry<'a>>,
use error::Result; extname = StoreIdGetIteratorExtension,
extfnname = into_get_iter,
pub struct StoreRetrieveIterator<'a>(StoreIdIterator, &'a Store); fun = |id: StoreId, store: &'a Store| store.get(id)
}
impl<'a> Iterator for StoreRetrieveIterator<'a> {
type Item = Result<FileLockEntry<'a>>; mk_iterator_mod! {
modname = retrieve,
fn next(&mut self) -> Option<Self::Item> { itername = StoreRetrieveIterator,
self.0.next().map(|id| self.1.retrieve(id)) iteryield = FileLockEntry<'a>,
} extname = StoreIdRetrieveIteratorExtension,
} extfnname = into_retrieve_iter,
fun = |id: StoreId, store: &'a Store| store.retrieve(id)
pub trait StoreIdRetrieveIteratorExtension<'a> {
fn into_retrieve_iter(self, store: &'a Store) -> StoreRetrieveIterator<'a>;
}
impl<'a> StoreIdRetrieveIteratorExtension<'a> for StoreIdIterator {
fn into_retrieve_iter(self, store: &'a Store) -> StoreRetrieveIterator<'a> {
StoreRetrieveIterator(self, store)
}
}
} }