Refactor FilterCompareLinkCountIter into FilterLinksIter

This commit is contained in:
Matthias Beyer 2016-10-17 15:45:04 +02:00
parent 19b60c3776
commit 7806cf34c1

View file

@ -125,18 +125,36 @@ pub mod iter {
DeleteUnlinkedIter(self) DeleteUnlinkedIter(self)
} }
/// Turn this iterator into a FilterCompareLinkCountIter, which filters out the unlinked /// Turn this iterator into a FilterLinksIter that removes all entries that are not linked
/// entries. /// to any other entry, by filtering them out the iterator.
pub fn without_unlinked(self) -> FilterCompareLinkCountIter<'a> { ///
FilterCompareLinkCountIter(gi, |u: usize| u > n) /// This does _not_ remove the entries from the store.
pub fn without_unlinked(self) -> FilterLinksIter<'a> {
FilterLinksIter::new(self, Box::new(|links: &[Link]| links.len() > 0))
} }
pub fn with_less_than_n_links(self, n: usize) -> FilterCompareLinkCountIter<'a> { /// Turn this iterator into a FilterLinksIter that removes all entries that have less than
FilterCompareLinkCountIter(gi, |u: usize| u < n) /// `n` links to any other entries.
///
/// This does _not_ remove the entries from the store.
pub fn with_less_than_n_links(self, n: usize) -> FilterLinksIter<'a> {
FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() < n))
} }
pub fn with_more_than_n_links(self, n: usize) -> FilterMoreThanIter<'a> { /// Turn this iterator into a FilterLinksIter that removes all entries that have more than
FilterMoreThanIter(self, n) /// `n` links to any other entries.
///
/// This does _not_ remove the entries from the store.
pub fn with_more_than_n_links(self, n: usize) -> FilterLinksIter<'a> {
FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() > n))
}
/// Turn this iterator into a FilterLinksIter that removes all entries where the predicate
/// `F` returns false
///
/// This does _not_ remove the entries from the store.
pub fn filtered_for_links(self, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> {
FilterLinksIter::new(self, f)
} }
pub fn store(&self) -> &Store { pub fn store(&self) -> &Store {
@ -161,10 +179,15 @@ pub mod iter {
/// ///
/// If the function F returns `false` for the number of links, the entry is ignored, else it is /// If the function F returns `false` for the number of links, the entry is ignored, else it is
/// taken. /// taken.
struct FilterCompareLinkCountIter<'a, F>(GetIter<'a>, F) pub struct FilterLinksIter<'a>(GetIter<'a>, Box<Fn(&[Link]) -> bool>);
where F: FnOnce(usize) -> bool;
impl<'a> Iterator for FilterCompareLinkCountIter<'a> { impl<'a> FilterLinksIter<'a> {
pub fn new(gi: GetIter<'a>, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> {
FilterLinksIter(gi, f)
}
}
impl<'a> Iterator for FilterLinksIter<'a> {
type Item = Result<FileLockEntry<'a>>; type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -176,9 +199,9 @@ pub mod iter {
let links = match fle.get_internal_links().map_err_into(LEK::StoreReadError) let links = match fle.get_internal_links().map_err_into(LEK::StoreReadError)
{ {
Err(e) => return Some(Err(e)), Err(e) => return Some(Err(e)),
Ok(links) => links, Ok(links) => links.collect::<Vec<_>>(),
}; };
if !self.1(links.count()) { if !(self.1)(&links) {
continue; continue;
} else { } else {
return Some(Ok(fle)); return Some(Ok(fle));