From aae9b15675bb6ec10041ade66959f731a6ea56fe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 26 Sep 2017 18:19:55 +0200 Subject: Add WithoutFilter iterator --- src/iter.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/iter.rs b/src/iter.rs index 4f178ed..0b3688f 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -127,6 +127,53 @@ impl EveryFilter for I } } +pub struct WithoutIter(I, M) + where I: Iterator>, + M: Matcher; + +impl WithoutIter + where I: Iterator>, + M: Matcher +{ + fn new(i: I, m: M) -> WithoutIter { + WithoutIter(i, m) + } +} + +impl Iterator for WithoutIter + where I: Iterator>, + M: Matcher +{ + type Item = Result; + + fn next(&mut self) -> Option { + loop { + match self.0.next() { + None => return None, + Some(Err(e)) => return Some(Err(e)), + Some(Ok(tt)) => match self.1.matches(&tt) { + Ok(false) => return Some(Ok(tt)), + Ok(true) => continue, + Err(e) => return Some(Err(e)), + } + } + } + } +} + +pub trait WithoutFilter : Iterator> + Sized { + fn without(self, M) -> WithoutIter; +} + +impl WithoutFilter for I + where I: Iterator>, + M: Matcher +{ + fn without(self, matcher: M) -> WithoutIter { + WithoutIter::new(self, matcher) + } +} + pub mod extensions { use timetype::TimeType as TT; use super::Iter; @@ -394,5 +441,19 @@ mod type_tests_filter_interface { .take(12) .collect::>(); } + + #[test] + fn test_compile_skip() { + // This test is solely to check whether this compiles and the API is nice + let v = TimeType::today() + .daily(1) + .unwrap() + .take(20) + .every(::indicator::Day::Monday) + .without(::indicator::Day::Monday) + .collect::>(); + + assert_eq!(0, v.len()); + } } -- cgit v1.2.3