Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itertools-0.10.5/src/peek_nth.rs
Line
Count
Source
1
use crate::size_hint;
2
use crate::PeekingNext;
3
use alloc::collections::VecDeque;
4
use std::iter::Fuse;
5
6
/// See [`peek_nth()`] for more information.
7
#[derive(Clone, Debug)]
8
pub struct PeekNth<I>
9
where
10
    I: Iterator,
11
{
12
    iter: Fuse<I>,
13
    buf: VecDeque<I::Item>,
14
}
15
16
/// A drop-in replacement for [`std::iter::Peekable`] which adds a `peek_nth`
17
/// method allowing the user to `peek` at a value several iterations forward
18
/// without advancing the base iterator.
19
///
20
/// This differs from `multipeek` in that subsequent calls to `peek` or
21
/// `peek_nth` will always return the same value until `next` is called
22
/// (making `reset_peek` unnecessary).
23
0
pub fn peek_nth<I>(iterable: I) -> PeekNth<I::IntoIter>
24
0
where
25
0
    I: IntoIterator,
26
{
27
0
    PeekNth {
28
0
        iter: iterable.into_iter().fuse(),
29
0
        buf: VecDeque::new(),
30
0
    }
31
0
}
32
33
impl<I> PeekNth<I>
34
where
35
    I: Iterator,
36
{
37
    /// Works exactly like the `peek` method in `std::iter::Peekable`
38
0
    pub fn peek(&mut self) -> Option<&I::Item> {
39
0
        self.peek_nth(0)
40
0
    }
41
42
    /// Returns a reference to the `nth` value without advancing the iterator.
43
    ///
44
    /// # Examples
45
    ///
46
    /// Basic usage:
47
    ///
48
    /// ```rust
49
    /// use itertools::peek_nth;
50
    ///
51
    /// let xs = vec![1,2,3];
52
    /// let mut iter = peek_nth(xs.iter());
53
    ///
54
    /// assert_eq!(iter.peek_nth(0), Some(&&1));
55
    /// assert_eq!(iter.next(), Some(&1));
56
    ///
57
    /// // The iterator does not advance even if we call `peek_nth` multiple times
58
    /// assert_eq!(iter.peek_nth(0), Some(&&2));
59
    /// assert_eq!(iter.peek_nth(1), Some(&&3));
60
    /// assert_eq!(iter.next(), Some(&2));
61
    ///
62
    /// // Calling `peek_nth` past the end of the iterator will return `None`
63
    /// assert_eq!(iter.peek_nth(1), None);
64
    /// ```
65
0
    pub fn peek_nth(&mut self, n: usize) -> Option<&I::Item> {
66
0
        let unbuffered_items = (n + 1).saturating_sub(self.buf.len());
67
68
0
        self.buf.extend(self.iter.by_ref().take(unbuffered_items));
69
70
0
        self.buf.get(n)
71
0
    }
72
}
73
74
impl<I> Iterator for PeekNth<I>
75
where
76
    I: Iterator,
77
{
78
    type Item = I::Item;
79
80
0
    fn next(&mut self) -> Option<Self::Item> {
81
0
        self.buf.pop_front().or_else(|| self.iter.next())
82
0
    }
83
84
0
    fn size_hint(&self) -> (usize, Option<usize>) {
85
0
        size_hint::add_scalar(self.iter.size_hint(), self.buf.len())
86
0
    }
87
}
88
89
impl<I> ExactSizeIterator for PeekNth<I> where I: ExactSizeIterator {}
90
91
impl<I> PeekingNext for PeekNth<I>
92
where
93
    I: Iterator,
94
{
95
0
    fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
96
0
    where
97
0
        F: FnOnce(&Self::Item) -> bool,
98
    {
99
0
        self.peek().filter(|item| accept(item))?;
100
0
        self.next()
101
0
    }
102
}