Skip to content

Commit 272aa21

Browse files
RepeatN: implement PeekingNext
1 parent 6c588cd commit 272aa21

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/peeking_take_while.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::PutBack;
22
#[cfg(feature = "use_alloc")]
33
use crate::PutBackN;
4+
use crate::RepeatN;
45
use std::iter::Peekable;
56

67
/// An iterator that allows peeking at an element before deciding to accept it.
@@ -91,6 +92,19 @@ where
9192
}
9293
}
9394

95+
impl<T: Clone> PeekingNext for RepeatN<T> {
96+
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
97+
where
98+
F: FnOnce(&Self::Item) -> bool,
99+
{
100+
let r = self.elt.as_ref()?;
101+
if !accept(r) {
102+
return None;
103+
}
104+
self.next()
105+
}
106+
}
107+
94108
/// An iterator adaptor that takes items while a closure returns `true`.
95109
///
96110
/// See [`.peeking_take_while()`](crate::Itertools::peeking_take_while)

src/repeatn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::iter::FusedIterator;
66
#[must_use = "iterators are lazy and do nothing unless consumed"]
77
#[derive(Clone, Debug)]
88
pub struct RepeatN<A> {
9-
elt: Option<A>,
9+
pub(crate) elt: Option<A>,
1010
n: usize,
1111
}
1212

0 commit comments

Comments
 (0)