| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 2 | 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#, FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#, Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#}; |
| 3 | |
| 4 | /// An iterator adaptor that wraps each element in an [`Position`]. |
| 5 | /// |
| 6 | /// Iterator element type is `(Position, I::Item)`. |
| 7 | /// |
| 8 | /// See [`.with_position()`](crate::Itertools::with_position) for more information. |
| 9 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 10 | pub struct WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> |
| 11 | where |
| 12 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 13 | { |
| 14 | handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first.: bool, |
| 15 | peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable.: Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#<Fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/Fuse#<I>>, |
| 16 | } |
| 17 | |
| 18 | impl<I> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> |
| 19 | where |
| 20 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 21 | Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#<Fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/Fuse#<I>>: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 22 | { |
| 23 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(WithPosition, handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first., peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable.); |
| 24 | } |
| 25 | |
| 26 | impl<I> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> |
| 27 | where |
| 28 | I: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 29 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 30 | { |
| 31 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first., peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable.); |
| 32 | } |
| 33 | |
| 34 | /// Create a new `WithPosition` iterator. |
| 35 | pub fn with_positionrust-analyzer cargo itertools 0.14.0 with_position/with_position().<I>(iter: I) -> WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> |
| 36 | where |
| 37 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 38 | { |
| 39 | WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition# { |
| 40 | handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first.: false, |
| 41 | peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable.: iter.fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#fuse().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// The first component of the value yielded by `WithPosition`. |
| 46 | /// Indicates the position of this element in the iterator results. |
| 47 | /// |
| 48 | /// See [`.with_position()`](crate::Itertools::with_position) for more information. |
| 49 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 50 | pub enum Positionrust-analyzer cargo itertools 0.14.0 with_position/Position# { |
| 51 | /// This is the first element. |
| 52 | Firstrust-analyzer cargo itertools 0.14.0 with_position/Position#First#, |
| 53 | /// This is neither the first nor the last element. |
| 54 | Middlerust-analyzer cargo itertools 0.14.0 with_position/Position#Middle#, |
| 55 | /// This is the last element. |
| 56 | Lastrust-analyzer cargo itertools 0.14.0 with_position/Position#Last#, |
| 57 | /// This is the only element. |
| 58 | Onlyrust-analyzer cargo itertools 0.14.0 with_position/Position#Only#, |
| 59 | } |
| 60 | |
| 61 | impl<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> { |
| 62 | type Itemrust-analyzer cargo itertools 0.14.0 with_position/impl#[`WithPosition<I>`][Iterator]Item# = (Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#, I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#); |
| 63 | |
| 64 | fn nextrust-analyzer cargo itertools 0.14.0 with_position/impl#[`WithPosition<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 with_position/impl#[`WithPosition<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 65 | match self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 66 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) => { |
| 67 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first. { |
| 68 | // Haven't seen the first item yet, and there is one to give. |
| 69 | self.handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first. = true; |
| 70 | // Peek to see if this is also the last item, |
| 71 | // in which case tag it as `Only`. |
| 72 | match self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() { |
| 73 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Firstrust-analyzer cargo itertools 0.14.0 with_position/Position#First#, item)), |
| 74 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Onlyrust-analyzer cargo itertools 0.14.0 with_position/Position#Only#, item)), |
| 75 | } |
| 76 | } else { |
| 77 | // Have seen the first item, and there's something left. |
| 78 | // Peek to see if this is the last item. |
| 79 | match self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() { |
| 80 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Middlerust-analyzer cargo itertools 0.14.0 with_position/Position#Middle#, item)), |
| 81 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Lastrust-analyzer cargo itertools 0.14.0 with_position/Position#Last#, item)), |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | // Iterator is finished. |
| 86 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn size_hintrust-analyzer cargo itertools 0.14.0 with_position/impl#[`WithPosition<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 91 | self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]size_hint().() |
| 92 | } |
| 93 | |
| 94 | fn foldrust-analyzer cargo itertools 0.14.0 with_position/impl#[`WithPosition<I>`][Iterator]fold().<B, F>(mut self, mut init: B, mut f: F) -> B |
| 95 | where |
| 96 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(B, Selfrust-analyzer cargo itertools 0.14.0 with_position/impl#[`WithPosition<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> B, |
| 97 | { |
| 98 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut head) = self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 99 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.handled_firstrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#handled_first. { |
| 100 | // The current head is `First` or `Only`, |
| 101 | // it depends if there is another item or not. |
| 102 | match self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 103 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(second) => { |
| 104 | let first = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(&mut head, second); |
| 105 | init = f(init, (Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Firstrust-analyzer cargo itertools 0.14.0 with_position/Position#First#, first)); |
| 106 | } |
| 107 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return f(init, (Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Onlyrust-analyzer cargo itertools 0.14.0 with_position/Position#Only#, head)), |
| 108 | } |
| 109 | } |
| 110 | // Have seen the first item, and there's something left. |
| 111 | init = self.peekablerust-analyzer cargo itertools 0.14.0 with_position/WithPosition#peekable..foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]fold().(init, |acc, mut item| { |
| 112 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/swap().(&mut head, &mut item); |
| 113 | f(acc, (Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Middlerust-analyzer cargo itertools 0.14.0 with_position/Position#Middle#, item)) |
| 114 | }); |
| 115 | // The "head" is now the last item. |
| 116 | init = f(init, (Positionrust-analyzer cargo itertools 0.14.0 with_position/Position#::Lastrust-analyzer cargo itertools 0.14.0 with_position/Position#Last#, head)); |
| 117 | } |
| 118 | init |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<I> ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# for WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> where I: ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# {} |
| 123 | |
| 124 | impl<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for WithPositionrust-analyzer cargo itertools 0.14.0 with_position/WithPosition#<I> {} |