Last active
April 13, 2017 23:37
-
-
Save U007D/ce24a7f55569bd5f4f94ba2668370c14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait MyFilter: Iterator { | |
fn my_filter_arg(self, arg: Self::Item) -> MyFilterArgState<Self> where Self: Sized, { | |
MyFilterArgState { iter: self, arg: arg } | |
} | |
fn my_filter<P>(self, predicate: P) -> MyFilterState<Self, P> where Self: Sized, | |
P: Fn(&Self::Item) -> bool, | |
Self::Item: PartialEq, { | |
MyFilterState { iter: self, predicate: predicate } | |
} | |
} | |
impl<I> MyFilter for I where I: Iterator, {} | |
struct MyFilterArgState<I> where I: Iterator, { | |
iter: I, | |
arg: I::Item, | |
} | |
impl<I> Iterator for MyFilterArgState<I> where I: Iterator, | |
I::Item: PartialEq, { | |
type Item = I::Item; | |
fn next(&mut self) -> Option<Self::Item> { | |
while let Some(v) = self.iter.next() { | |
if v != self.arg { return Some(v); } | |
} | |
None | |
} | |
} | |
struct MyFilterState<I, P> { | |
iter: I, | |
predicate: P, | |
} | |
impl<I, P> Iterator for MyFilterState<I, P> where I: Iterator, | |
P: Fn(&I::Item) -> bool, | |
I::Item: PartialEq, { | |
type Item = I::Item; | |
fn next(&mut self) -> Option<Self::Item> { | |
while let Some(v) = self.iter.next() { | |
if (self.predicate)(&v) { return Some(v); } | |
} | |
None | |
} | |
} | |
#[cfg(test)] | |
mod unit_tests; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment