Skip to content

Instantly share code, notes, and snippets.

@joe-askattest
Created February 12, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joe-askattest/46e6537d03e931fc942fcddd4499d3b5 to your computer and use it in GitHub Desktop.
Save joe-askattest/46e6537d03e931fc942fcddd4499d3b5 to your computer and use it in GitHub Desktop.
// 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