From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/itertools-0.8.0/src/repeatn.rs | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 third_party/rust/itertools-0.8.0/src/repeatn.rs (limited to 'third_party/rust/itertools-0.8.0/src/repeatn.rs') diff --git a/third_party/rust/itertools-0.8.0/src/repeatn.rs b/third_party/rust/itertools-0.8.0/src/repeatn.rs new file mode 100644 index 0000000000..1c7c310014 --- /dev/null +++ b/third_party/rust/itertools-0.8.0/src/repeatn.rs @@ -0,0 +1,54 @@ + +/// An iterator that produces *n* repetitions of an element. +/// +/// See [`repeat_n()`](../fn.repeat_n.html) for more information. +#[must_use = "iterators are lazy and do nothing unless consumed"] +#[derive(Debug)] +pub struct RepeatN { + elt: Option, + n: usize, +} + +/// Create an iterator that produces `n` repetitions of `element`. +pub fn repeat_n(element: A, n: usize) -> RepeatN + where A: Clone, +{ + if n == 0 { + RepeatN { elt: None, n: n, } + } else { + RepeatN { elt: Some(element), n: n, } + } +} + +impl Iterator for RepeatN + where A: Clone +{ + type Item = A; + + fn next(&mut self) -> Option { + if self.n > 1 { + self.n -= 1; + self.elt.as_ref().cloned() + } else { + self.n = 0; + self.elt.take() + } + } + + fn size_hint(&self) -> (usize, Option) { + (self.n, Some(self.n)) + } +} + +impl DoubleEndedIterator for RepeatN + where A: Clone +{ + #[inline] + fn next_back(&mut self) -> Option { + self.next() + } +} + +impl ExactSizeIterator for RepeatN + where A: Clone +{} -- cgit v1.2.3