| 1 | //! "Diff"ing iterators for caching elements to sequential collections without requiring the new |
| 2 | //! elements' iterator to be `Clone`. |
| 3 | //! |
| 4 | //! [`Diff`] (produced by the [`diff_with`] function) |
| 5 | //! describes the difference between two non-`Clone` iterators `I` and `J` after breaking ASAP from |
| 6 | //! a lock-step comparison. |
| 7 | |
| 8 | 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/; |
| 9 | |
| 10 | use craterust-analyzer cargo itertools 0.14.0 crate/::freerust-analyzer cargo itertools 0.14.0 free/::put_backrust-analyzer cargo itertools 0.14.0 adaptors/put_back().; |
| 11 | use craterust-analyzer cargo itertools 0.14.0 crate/::structsrust-analyzer cargo itertools 0.14.0 structs/::PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#; |
| 12 | |
| 13 | /// A type returned by the [`diff_with`] function. |
| 14 | /// |
| 15 | /// `Diff` represents the way in which the elements yielded by the iterator `I` differ to some |
| 16 | /// iterator `J`. |
| 17 | pub enum Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#<I, J> |
| 18 | where |
| 19 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 20 | J: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 21 | { |
| 22 | /// The index of the first non-matching element along with both iterator's remaining elements |
| 23 | /// starting with the first mis-match. |
| 24 | FirstMismatchrust-analyzer cargo itertools 0.14.0 diff/Diff#FirstMismatch#(usize, PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<I>, PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<J>), |
| 25 | /// The total number of elements that were in `J` along with the remaining elements of `I`. |
| 26 | Shorterrust-analyzer cargo itertools 0.14.0 diff/Diff#Shorter#(usize, PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<I>), |
| 27 | /// The total number of elements that were in `I` along with the remaining elements of `J`. |
| 28 | Longerrust-analyzer cargo itertools 0.14.0 diff/Diff#Longer#(usize, PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<J>), |
| 29 | } |
| 30 | |
| 31 | impl<I, J> 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 Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#<I, J> |
| 32 | where |
| 33 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 34 | J: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 35 | PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<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#, |
| 36 | PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<J>: 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#, |
| 37 | { |
| 38 | fn fmtrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 39 | match self { |
| 40 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Debug]::FirstMismatchrust-analyzer cargo itertools 0.14.0 diff/Diff#FirstMismatch#(idx, i, j) => f |
| 41 | .debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("FirstMismatch") |
| 42 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(idx) |
| 43 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(i) |
| 44 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(j) |
| 45 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().(), |
| 46 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Debug]::Shorterrust-analyzer cargo itertools 0.14.0 diff/Diff#Shorter#(idx, i) => f.debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("Shorter").fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(idx).fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(i).finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().(), |
| 47 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Debug]::Longerrust-analyzer cargo itertools 0.14.0 diff/Diff#Longer#(idx, j) => f.debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("Longer").fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(idx).fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(j).finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().(), |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl<I, J> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#<I, J> |
| 53 | where |
| 54 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 55 | J: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 56 | PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<I>: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 57 | PutBackrust-analyzer cargo itertools 0.14.0 adaptors/PutBack#<J>: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 58 | { |
| 59 | fn clonerust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]clone().(&self) -> Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone] { |
| 60 | match self { |
| 61 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::FirstMismatchrust-analyzer cargo itertools 0.14.0 diff/Diff#FirstMismatch#(idx, i, j) => Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::FirstMismatchrust-analyzer cargo itertools 0.14.0 diff/Diff#FirstMismatch#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().idx, i.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(), j.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()), |
| 62 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::Shorterrust-analyzer cargo itertools 0.14.0 diff/Diff#Shorter#(idx, i) => Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::Shorterrust-analyzer cargo itertools 0.14.0 diff/Diff#Shorter#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().idx, i.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()), |
| 63 | Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::Longerrust-analyzer cargo itertools 0.14.0 diff/Diff#Longer#(idx, j) => Selfrust-analyzer cargo itertools 0.14.0 diff/impl#[`Diff<I, J>`][Clone]::Longerrust-analyzer cargo itertools 0.14.0 diff/Diff#Longer#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().idx, j.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Compares every element yielded by both `i` and `j` with the given function in lock-step and |
| 69 | /// returns a [`Diff`] which describes how `j` differs from `i`. |
| 70 | /// |
| 71 | /// If the number of elements yielded by `j` is less than the number of elements yielded by `i`, |
| 72 | /// the number of `j` elements yielded will be returned along with `i`'s remaining elements as |
| 73 | /// `Diff::Shorter`. |
| 74 | /// |
| 75 | /// If the two elements of a step differ, the index of those elements along with the remaining |
| 76 | /// elements of both `i` and `j` are returned as `Diff::FirstMismatch`. |
| 77 | /// |
| 78 | /// If `i` becomes exhausted before `j` becomes exhausted, the number of elements in `i` along with |
| 79 | /// the remaining `j` elements will be returned as `Diff::Longer`. |
| 80 | pub fn diff_withrust-analyzer cargo itertools 0.14.0 diff/diff_with().<I, J, F>(i: I, j: J, mut is_equal: F) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#<I::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#, J::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#>> |
| 81 | where |
| 82 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 83 | J: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 84 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#, &J::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#) -> bool, |
| 85 | { |
| 86 | let mut i = i.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().(); |
| 87 | let mut j = j.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().(); |
| 88 | let mut idx = 0; |
| 89 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i_elem) = i.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 90 | match j.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 91 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#::Shorterrust-analyzer cargo itertools 0.14.0 diff/Diff#Shorter#(idx, put_backrust-analyzer cargo itertools 0.14.0 adaptors/put_back().(i).with_valuerust-analyzer cargo itertools 0.14.0 adaptors/impl#[`PutBack<I>`]with_value().(i_elem))), |
| 92 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(j_elem) => { |
| 93 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_equal(&i_elem, &j_elem) { |
| 94 | let remaining_i = put_backrust-analyzer cargo itertools 0.14.0 adaptors/put_back().(i).with_valuerust-analyzer cargo itertools 0.14.0 adaptors/impl#[`PutBack<I>`]with_value().(i_elem); |
| 95 | let remaining_j = put_backrust-analyzer cargo itertools 0.14.0 adaptors/put_back().(j).with_valuerust-analyzer cargo itertools 0.14.0 adaptors/impl#[`PutBack<I>`]with_value().(j_elem); |
| 96 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#::FirstMismatchrust-analyzer cargo itertools 0.14.0 diff/Diff#FirstMismatch#(idx, remaining_i, remaining_j)); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 101 | } |
| 102 | j.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() |
| 103 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|j_elem| Diffrust-analyzer cargo itertools 0.14.0 diff/Diff#::Longerrust-analyzer cargo itertools 0.14.0 diff/Diff#Longer#(idx, put_backrust-analyzer cargo itertools 0.14.0 adaptors/put_back().(j).with_valuerust-analyzer cargo itertools 0.14.0 adaptors/impl#[`PutBack<I>`]with_value().(j_elem))) |
| 104 | } |