diff --git a/src/repeatn.rs b/src/repeatn.rs index 512d057f3..539c42615 100644 --- a/src/repeatn.rs +++ b/src/repeatn.rs @@ -44,6 +44,20 @@ where fn size_hint(&self) -> (usize, Option) { (self.n, Some(self.n)) } + + fn fold(self, mut init: B, mut f: F) -> B + where + F: FnMut(B, Self::Item) -> B, + { + match self { + Self { elt: Some(elt), n } => { + debug_assert!(n > 0); + init = (1..n).map(|_| elt.clone()).fold(init, &mut f); + f(init, elt) + } + _ => init, + } + } } impl DoubleEndedIterator for RepeatN @@ -54,6 +68,14 @@ where fn next_back(&mut self) -> Option { self.next() } + + #[inline] + fn rfold(self, init: B, f: F) -> B + where + F: FnMut(B, Self::Item) -> B, + { + self.fold(init, f) + } } impl ExactSizeIterator for RepeatN where A: Clone {}