| 1 | use craterust-analyzer cargo itertools 0.14.0 crate/::size_hintrust-analyzer cargo itertools 0.14.0 size_hint/; |
| 2 | use craterust-analyzer cargo itertools 0.14.0 crate/::PeekingNextrust-analyzer cargo itertools 0.14.0 peeking_take_while/PeekingNext#; |
| 3 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/Fuse#; |
| 5 | |
| 6 | /// See [`peek_nth()`] for more information. |
| 7 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 8 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 9 | pub struct PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I> |
| 10 | where |
| 11 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 12 | { |
| 13 | iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter.: Fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/Fuse#<I>, |
| 14 | bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>, |
| 15 | } |
| 16 | |
| 17 | /// A drop-in replacement for [`std::iter::Peekable`] which adds a `peek_nth` |
| 18 | /// method allowing the user to `peek` at a value several iterations forward |
| 19 | /// without advancing the base iterator. |
| 20 | /// |
| 21 | /// This differs from `multipeek` in that subsequent calls to `peek` or |
| 22 | /// `peek_nth` will always return the same value until `next` is called |
| 23 | /// (making `reset_peek` unnecessary). |
| 24 | pub fn peek_nthrust-analyzer cargo itertools 0.14.0 peek_nth/peek_nth().<I>(iterable: I) -> PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#> |
| 25 | where |
| 26 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 27 | { |
| 28 | PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth# { |
| 29 | iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter.: iterable.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#fuse().(), |
| 30 | bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T>`]new().(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl<I> PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I> |
| 35 | where |
| 36 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 37 | { |
| 38 | /// Works exactly like the `peek` method in [`std::iter::Peekable`]. |
| 39 | pub fn peekrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 40 | self.peek_nthrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek_nth().(0) |
| 41 | } |
| 42 | |
| 43 | /// Works exactly like the `peek_mut` method in [`std::iter::Peekable`]. |
| 44 | pub fn peek_mutrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek_mut().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 45 | self.peek_nth_mutrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek_nth_mut().(0) |
| 46 | } |
| 47 | |
| 48 | /// Returns a reference to the `nth` value without advancing the iterator. |
| 49 | /// |
| 50 | /// # Examples |
| 51 | /// |
| 52 | /// Basic usage: |
| 53 | /// |
| 54 | /// ``` |
| 55 | /// use itertools::peek_nth; |
| 56 | /// |
| 57 | /// let xs = vec![1, 2, 3]; |
| 58 | /// let mut iter = peek_nth(xs.into_iter()); |
| 59 | /// |
| 60 | /// assert_eq!(iter.peek_nth(0), Some(&1)); |
| 61 | /// assert_eq!(iter.next(), Some(1)); |
| 62 | /// |
| 63 | /// // The iterator does not advance even if we call `peek_nth` multiple times |
| 64 | /// assert_eq!(iter.peek_nth(0), Some(&2)); |
| 65 | /// assert_eq!(iter.peek_nth(1), Some(&3)); |
| 66 | /// assert_eq!(iter.next(), Some(2)); |
| 67 | /// |
| 68 | /// // Calling `peek_nth` past the end of the iterator will return `None` |
| 69 | /// assert_eq!(iter.peek_nth(1), None); |
| 70 | /// ``` |
| 71 | pub fn peek_nthrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek_nth().(&mut self, n: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 72 | let unbuffered_items = (n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]len().()); |
| 73 | |
| 74 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`][`Extend<T>`]extend().(self.iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter..by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(unbuffered_items)); |
| 75 | |
| 76 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..getrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]get().(n) |
| 77 | } |
| 78 | |
| 79 | /// Returns a mutable reference to the `nth` value without advancing the iterator. |
| 80 | /// |
| 81 | /// # Examples |
| 82 | /// |
| 83 | /// Basic usage: |
| 84 | /// |
| 85 | /// ``` |
| 86 | /// use itertools::peek_nth; |
| 87 | /// |
| 88 | /// let xs = vec![1, 2, 3, 4, 5]; |
| 89 | /// let mut iter = peek_nth(xs.into_iter()); |
| 90 | /// |
| 91 | /// assert_eq!(iter.peek_nth_mut(0), Some(&mut 1)); |
| 92 | /// assert_eq!(iter.next(), Some(1)); |
| 93 | /// |
| 94 | /// // The iterator does not advance even if we call `peek_nth_mut` multiple times |
| 95 | /// assert_eq!(iter.peek_nth_mut(0), Some(&mut 2)); |
| 96 | /// assert_eq!(iter.peek_nth_mut(1), Some(&mut 3)); |
| 97 | /// assert_eq!(iter.next(), Some(2)); |
| 98 | /// |
| 99 | /// // Peek into the iterator and set the value behind the mutable reference. |
| 100 | /// if let Some(p) = iter.peek_nth_mut(1) { |
| 101 | /// assert_eq!(*p, 4); |
| 102 | /// *p = 9; |
| 103 | /// } |
| 104 | /// |
| 105 | /// // The value we put in reappears as the iterator continues. |
| 106 | /// assert_eq!(iter.next(), Some(3)); |
| 107 | /// assert_eq!(iter.next(), Some(9)); |
| 108 | /// |
| 109 | /// // Calling `peek_nth_mut` past the end of the iterator will return `None` |
| 110 | /// assert_eq!(iter.peek_nth_mut(1), None); |
| 111 | /// ``` |
| 112 | pub fn peek_nth_mutrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek_nth_mut().(&mut self, n: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 113 | let unbuffered_items = (n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]len().()); |
| 114 | |
| 115 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`][`Extend<T>`]extend().(self.iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter..by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(unbuffered_items)); |
| 116 | |
| 117 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..get_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]get_mut().(n) |
| 118 | } |
| 119 | |
| 120 | /// Works exactly like the `next_if` method in [`std::iter::Peekable`]. |
| 121 | pub fn next_ifrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]next_if().(&mut self, func: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> bool) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 122 | match self.nextrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]next().() { |
| 123 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) if func(&item) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item), |
| 124 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) => { |
| 125 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..push_frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]push_front().(item); |
| 126 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 127 | } |
| 128 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Works exactly like the `next_if_eq` method in [`std::iter::Peekable`]. |
| 133 | pub fn next_if_eqrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]next_if_eq().<T>(&mut self, expected: &T) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> |
| 134 | where |
| 135 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 136 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<T>, |
| 137 | { |
| 138 | self.next_ifrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]next_if().(|next| next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().expected) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I> |
| 143 | where |
| 144 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 145 | { |
| 146 | type Itemrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 147 | |
| 148 | fn nextrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 149 | self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..pop_frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]pop_front().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| self.iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/impl#[`Fuse<I>`][Iterator]next().()) |
| 150 | } |
| 151 | |
| 152 | fn size_hintrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 153 | size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::add_scalarrust-analyzer cargo itertools 0.14.0 size_hint/add_scalar().(self.iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter..size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/impl#[`Fuse<I>`][Iterator]size_hint().(), self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]len().()) |
| 154 | } |
| 155 | |
| 156 | fn foldrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]fold().<B, F>(self, mut init: B, mut f: F) -> B |
| 157 | where |
| 158 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(B, Selfrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> B, |
| 159 | { |
| 160 | init = self.bufrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#buf..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`][IntoIterator]into_iter().().foldrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/into_iter/impl#[`IntoIter<T, A>`][Iterator]fold().(init, &mut f); |
| 161 | self.iterrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#iter..foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/impl#[`Fuse<I>`][Iterator]fold().(init, f) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl<I> ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# for PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I> where I: ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# {} |
| 166 | |
| 167 | impl<I> PeekingNextrust-analyzer cargo itertools 0.14.0 peeking_take_while/PeekingNext# for PeekNthrust-analyzer cargo itertools 0.14.0 peek_nth/PeekNth#<I> |
| 168 | where |
| 169 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 170 | { |
| 171 | fn peeking_nextrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][PeekingNext]peeking_next().<F>(&mut self, accept: F) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][PeekingNext]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> |
| 172 | where |
| 173 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&Selfrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][PeekingNext]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> bool, |
| 174 | { |
| 175 | self.peekrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`]peek().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]filter().(|item| accept(item))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 176 | self.nextrust-analyzer cargo itertools 0.14.0 peek_nth/impl#[`PeekNth<I>`][Iterator]next().() |
| 177 | } |
| 178 | } |