Skip to content

Commit

Permalink
add next_if and next_if_eq to PeekNth, such that it matches the funct…
Browse files Browse the repository at this point in the history
…ionality found in std::iter::Peekable
  • Loading branch information
herlev authored and Philippe-Cholet committed Oct 10, 2023
1 parent a948086 commit 6d39221
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/peek_nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ where

self.buf.get_mut(n)
}

/// Works exactly like the `next_if` method in `std::iter::Peekable`
pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> {
match self.peek() {
Some(m) if func(&m) => self.next(),
_ => None,
}
}

/// Works exactly like the `next_if_eq` method in `std::iter::Peekable`
pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
where
T: ?Sized,
I::Item: PartialEq<T>,
{
self.next_if(|next| next == expected)
}
}

impl<I> Iterator for PeekNth<I>
Expand Down

0 comments on commit 6d39221

Please sign in to comment.