Created
February 12, 2019 07:32
-
-
Save joe-askattest/46e6537d03e931fc942fcddd4499d3b5 to your computer and use it in GitHub Desktop.
This file contains 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
// example usable | |
for (child, next) in values.iter().with_next() { | |
// do stuff | |
} | |
struct WithNext<I> | |
where | |
I: Iterator, | |
{ | |
underlying: Peekable<I>, | |
} | |
impl<I> Iterator for WithNext<I> | |
where | |
I: Iterator, | |
I::Item: Eq + Copy, | |
{ | |
type Item = (I::Item, Option<&I::Item>); | |
fn next(&mut self) -> Option<Self::Item> { | |
if let Some(next) = self.underlying.next() { | |
Some((next, self.underlying.peek())) | |
} else { | |
None | |
} | |
} | |
} | |
trait WithNextExt: Iterator { | |
fn with_next(self) -> WithNext<Self> | |
where | |
Self::Item: Eq + Clone, | |
Self: Sized, | |
{ | |
WithNext { | |
underlying: self.peekable(), | |
} | |
} | |
} | |
impl<I: Iterator> WithNextExt for I {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment