]>
git.proxmox.com Git - rustc.git/blob - vendor/itertools-0.7.8/src/multipeek_impl.rs
4 use std
::collections
::VecDeque
;
8 /// See [`multipeek()`](../fn.multipeek.html) for more information.
9 #[derive(Clone, Debug)]
10 pub struct MultiPeek
<I
>
14 buf
: VecDeque
<I
::Item
>,
18 /// An iterator adaptor that allows the user to peek at multiple `.next()`
19 /// values without advancing the base iterator.
20 pub fn multipeek
<I
>(iterable
: I
) -> MultiPeek
<I
::IntoIter
>
24 iter
: iterable
.into_iter().fuse(),
33 /// Reset the peeking “cursor”
34 pub fn reset_peek(&mut self) {
39 impl<I
: Iterator
> MultiPeek
<I
> {
40 /// Works exactly like `.next()` with the only difference that it doesn't
41 /// advance itself. `.peek()` can be called multiple times, to peek
43 pub fn peek(&mut self) -> Option
<&I
::Item
> {
44 let ret
= if self.index
< self.buf
.len() {
45 Some(&self.buf
[self.index
])
47 match self.iter
.next() {
49 self.buf
.push_back(x
);
50 Some(&self.buf
[self.index
])
61 impl<I
> PeekingNext
for MultiPeek
<I
>
64 fn peeking_next
<F
>(&mut self, accept
: F
) -> Option
<Self::Item
>
65 where F
: FnOnce(&Self::Item
) -> bool
67 if self.buf
.is_empty() {
68 if let Some(r
) = self.peek() {
69 if !accept(r
) { return None }
72 if let Some(r
) = self.buf
.get(0) {
73 if !accept(r
) { return None }
80 impl<I
> Iterator
for MultiPeek
<I
>
85 fn next(&mut self) -> Option
<I
::Item
> {
87 if self.buf
.is_empty() {
94 fn size_hint(&self) -> (usize, Option
<usize>) {
95 size_hint
::add_scalar(self.iter
.size_hint(), self.buf
.len())
100 impl<I
> ExactSizeIterator
for MultiPeek
<I
>
101 where I
: ExactSizeIterator