From 3a4f59eafe6e8a4e00d0a3aa0fe1853a3351adaa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 11 Nov 2017 11:19:11 +0100 Subject: Add "Times" iterator helper type --- src/iter.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/iter.rs b/src/iter.rs index 64c6ba1..7399de5 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -222,6 +222,53 @@ impl Until for I } } +pub struct TimesIter + where I: Iterator> +{ + inner: I, + times: i64, + count: i64, +} + +impl TimesIter + where I: Iterator> +{ + fn new(i: I, times: i64) -> TimesIter { + TimesIter { + inner: i, + times: times, + count: 0, + } + } +} + +impl Iterator for TimesIter + where I: Iterator> +{ + type Item = Result; + + fn next(&mut self) -> Option { + if self.times == self.count { + None + } else { + self.count += 1; + self.inner.next() + } + } +} + +pub trait Times : Iterator> + Sized { + fn times(self, i64) -> TimesIter; +} + +impl Times for I + where I: Iterator> +{ + fn times(self, times: i64) -> TimesIter { + TimesIter::new(self, times) + } +} + pub mod extensions { use timetype::TimeType as TT; use super::Iter; -- cgit v1.2.3