Skip to content

Commit

Permalink
RepeatN: implement PeekingNext
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 18, 2024
1 parent 6c588cd commit 543c30c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/peeking_take_while.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::PutBack;
#[cfg(feature = "use_alloc")]
use crate::PutBackN;
use crate::RepeatN;
use std::iter::Peekable;

/// An iterator that allows peeking at an element before deciding to accept it.
Expand Down Expand Up @@ -91,6 +92,19 @@ where
}
}

impl<T: Clone> PeekingNext for RepeatN<T> {
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
where
F: FnOnce(&Self::Item) -> bool,
{
let r = self.elt.as_ref()?;
if !accept(r) {
return None;
}
self.next()
}
}

/// An iterator adaptor that takes items while a closure returns `true`.
///
/// See [`.peeking_take_while()`](crate::Itertools::peeking_take_while)
Expand Down
2 changes: 1 addition & 1 deletion src/repeatn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::iter::FusedIterator;
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct RepeatN<A> {
elt: Option<A>,
pub(crate) elt: Option<A>,
n: usize,
}

Expand Down

0 comments on commit 543c30c

Please sign in to comment.