Coverage Report

Created: 2025-12-28 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itertools-0.10.5/src/pad_tail.rs
Line
Count
Source
1
use std::iter::{Fuse, FusedIterator};
2
use crate::size_hint;
3
4
/// An iterator adaptor that pads a sequence to a minimum length by filling
5
/// missing elements using a closure.
6
///
7
/// Iterator element type is `I::Item`.
8
///
9
/// See [`.pad_using()`](crate::Itertools::pad_using) for more information.
10
#[derive(Clone)]
11
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
12
pub struct PadUsing<I, F> {
13
    iter: Fuse<I>,
14
    min: usize,
15
    pos: usize,
16
    filler: F,
17
}
18
19
impl<I, F> std::fmt::Debug for PadUsing<I, F>
20
where
21
    I: std::fmt::Debug,
22
{
23
    debug_fmt_fields!(PadUsing, iter, min, pos);
24
}
25
26
/// Create a new `PadUsing` iterator.
27
0
pub fn pad_using<I, F>(iter: I, min: usize, filler: F) -> PadUsing<I, F>
28
0
    where I: Iterator,
29
0
          F: FnMut(usize) -> I::Item
30
{
31
0
    PadUsing {
32
0
        iter: iter.fuse(),
33
0
        min,
34
0
        pos: 0,
35
0
        filler,
36
0
    }
37
0
}
38
39
impl<I, F> Iterator for PadUsing<I, F>
40
    where I: Iterator,
41
          F: FnMut(usize) -> I::Item
42
{
43
    type Item = I::Item;
44
45
    #[inline]
46
0
    fn next(&mut self) -> Option<Self::Item> {
47
0
        match self.iter.next() {
48
            None => {
49
0
                if self.pos < self.min {
50
0
                    let e = Some((self.filler)(self.pos));
51
0
                    self.pos += 1;
52
0
                    e
53
                } else {
54
0
                    None
55
                }
56
            },
57
0
            e => {
58
0
                self.pos += 1;
59
0
                e
60
            }
61
        }
62
0
    }
63
64
0
    fn size_hint(&self) -> (usize, Option<usize>) {
65
0
        let tail = self.min.saturating_sub(self.pos);
66
0
        size_hint::max(self.iter.size_hint(), (tail, Some(tail)))
67
0
    }
68
}
69
70
impl<I, F> DoubleEndedIterator for PadUsing<I, F>
71
    where I: DoubleEndedIterator + ExactSizeIterator,
72
          F: FnMut(usize) -> I::Item
73
{
74
0
    fn next_back(&mut self) -> Option<Self::Item> {
75
0
        if self.min == 0 {
76
0
            self.iter.next_back()
77
0
        } else if self.iter.len() >= self.min {
78
0
            self.min -= 1;
79
0
            self.iter.next_back()
80
        } else {
81
0
            self.min -= 1;
82
0
            Some((self.filler)(self.min))
83
        }
84
0
    }
85
}
86
87
impl<I, F> ExactSizeIterator for PadUsing<I, F>
88
    where I: ExactSizeIterator,
89
          F: FnMut(usize) -> I::Item
90
{}
91
92
93
impl<I, F> FusedIterator for PadUsing<I, F>
94
    where I: FusedIterator,
95
          F: FnMut(usize) -> I::Item
96
{}