]> git.proxmox.com Git - rustc.git/blame - src/vendor/itertools/src/repeatn.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / vendor / itertools / src / repeatn.rs
CommitLineData
abe05a73
XL
1
2/// An iterator that produces *n* repetitions of an element.
3///
4/// See [`repeat_n()`](../fn.repeat_n.html) for more information.
2c00a5a8 5#[must_use = "iterators are lazy and do nothing unless consumed"]
abe05a73
XL
6pub struct RepeatN<A> {
7 elt: Option<A>,
8 n: usize,
9}
10
11/// Create an iterator that produces `n` repetitions of `element`.
12pub fn repeat_n<A>(element: A, n: usize) -> RepeatN<A>
13 where A: Clone,
14{
15 if n == 0 {
16 RepeatN { elt: None, n: n, }
17 } else {
18 RepeatN { elt: Some(element), n: n, }
19 }
20}
21
22impl<A> Iterator for RepeatN<A>
23 where A: Clone
24{
25 type Item = A;
26
27 fn next(&mut self) -> Option<Self::Item> {
28 if self.n > 1 {
29 self.n -= 1;
30 self.elt.as_ref().cloned()
31 } else {
32 self.n = 0;
33 self.elt.take()
34 }
35 }
36
37 fn size_hint(&self) -> (usize, Option<usize>) {
38 (self.n, Some(self.n))
39 }
40}
41
42impl<A> DoubleEndedIterator for RepeatN<A>
43 where A: Clone
44{
45 #[inline]
46 fn next_back(&mut self) -> Option<Self::Item> {
47 self.next()
48 }
49}
50
51impl<A> ExactSizeIterator for RepeatN<A>
52 where A: Clone
53{}