Coverage Report

Created: 2025-11-28 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itertools-0.11.0/src/repeatn.rs
Line
Count
Source
1
use std::iter::FusedIterator;
2
3
/// An iterator that produces *n* repetitions of an element.
4
///
5
/// See [`repeat_n()`](crate::repeat_n) for more information.
6
#[must_use = "iterators are lazy and do nothing unless consumed"]
7
#[derive(Clone, Debug)]
8
pub struct RepeatN<A> {
9
    elt: Option<A>,
10
    n: usize,
11
}
12
13
/// Create an iterator that produces `n` repetitions of `element`.
14
0
pub fn repeat_n<A>(element: A, n: usize) -> RepeatN<A>
15
0
    where A: Clone,
16
{
17
0
    if n == 0 {
18
0
        RepeatN { elt: None, n, }
19
    } else {
20
0
        RepeatN { elt: Some(element), n, }
21
    }
22
0
}
23
24
impl<A> Iterator for RepeatN<A>
25
    where A: Clone
26
{
27
    type Item = A;
28
29
0
    fn next(&mut self) -> Option<Self::Item> {
30
0
        if self.n > 1 {
31
0
            self.n -= 1;
32
0
            self.elt.as_ref().cloned()
33
        } else {
34
0
            self.n = 0;
35
0
            self.elt.take()
36
        }
37
0
    }
38
39
0
    fn size_hint(&self) -> (usize, Option<usize>) {
40
0
        (self.n, Some(self.n))
41
0
    }
42
}
43
44
impl<A> DoubleEndedIterator for RepeatN<A>
45
    where A: Clone
46
{
47
    #[inline]
48
0
    fn next_back(&mut self) -> Option<Self::Item> {
49
0
        self.next()
50
0
    }
51
}
52
53
impl<A> ExactSizeIterator for RepeatN<A>
54
    where A: Clone
55
{}
56
57
impl<A> FusedIterator for RepeatN<A>
58
    where A: Clone
59
{}