diff --git a/src/peeking_take_while.rs b/src/peeking_take_while.rs index b4852c1c9..b224907aa 100644 --- a/src/peeking_take_while.rs +++ b/src/peeking_take_while.rs @@ -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. @@ -91,6 +92,19 @@ where } } +impl PeekingNext for RepeatN { + fn peeking_next(&mut self, accept: F) -> Option + 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) diff --git a/src/repeatn.rs b/src/repeatn.rs index 539c42615..d86ad9fac 100644 --- a/src/repeatn.rs +++ b/src/repeatn.rs @@ -6,7 +6,7 @@ use std::iter::FusedIterator; #[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] pub struct RepeatN { - elt: Option, + pub(crate) elt: Option, n: usize, } diff --git a/tests/test_std.rs b/tests/test_std.rs index be163e98d..5d2864573 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -8,6 +8,7 @@ use crate::it::izip; use crate::it::multipeek; use crate::it::multizip; use crate::it::peek_nth; +use crate::it::repeat_n; use crate::it::ExactlyOneError; use crate::it::FoldWhile; use crate::it::Itertools; @@ -674,6 +675,21 @@ fn test_multipeek_peeking_next() { assert_eq!(mp.peek(), None); } +#[test] +fn test_repeat_n_peeking_next() { + use crate::it::PeekingNext; + let mut rn = repeat_n(0, 5); + assert_eq!(rn.peeking_next(|&x| x != 0), None); + assert_eq!(rn.peeking_next(|&x| x <= 0), Some(0)); + assert_eq!(rn.next(), Some(0)); + assert_eq!(rn.peeking_next(|&x| x <= 0), Some(0)); + assert_eq!(rn.peeking_next(|&x| x != 0), None); + assert_eq!(rn.peeking_next(|&x| x >= 0), Some(0)); + assert_eq!(rn.next(), Some(0)); + assert_eq!(rn.peeking_next(|&x| x <= 0), None); + assert_eq!(rn.next(), None); +} + #[test] fn test_peek_nth() { let nums = vec![1u8, 2, 3, 4, 5];