Skip to content

Commit 6d39221

Browse files
herlevPhilippe-Cholet
authored andcommitted
add next_if and next_if_eq to PeekNth, such that it matches the functionality found in std::iter::Peekable
1 parent a948086 commit 6d39221

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/peek_nth.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ where
115115

116116
self.buf.get_mut(n)
117117
}
118+
119+
/// Works exactly like the `next_if` method in `std::iter::Peekable`
120+
pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> {
121+
match self.peek() {
122+
Some(m) if func(&m) => self.next(),
123+
_ => None,
124+
}
125+
}
126+
127+
/// Works exactly like the `next_if_eq` method in `std::iter::Peekable`
128+
pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
129+
where
130+
T: ?Sized,
131+
I::Item: PartialEq<T>,
132+
{
133+
self.next_if(|next| next == expected)
134+
}
118135
}
119136

120137
impl<I> Iterator for PeekNth<I>

0 commit comments

Comments
 (0)