| 1 | #[cfg(feature = "use_std")] |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 3 | 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/::{Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# as FmtResultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result#}; |
| 4 | |
| 5 | 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/::ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator#; |
| 6 | |
| 7 | use eitherrust-analyzer cargo either 1.15.0 crate/::Eitherrust-analyzer cargo either 1.15.0 Either#; |
| 8 | |
| 9 | use craterust-analyzer cargo itertools 0.14.0 crate/::size_hintrust-analyzer cargo itertools 0.14.0 size_hint/; |
| 10 | |
| 11 | /// Iterator returned for the error case of `Itertools::exactly_one()` |
| 12 | /// This iterator yields exactly the same elements as the input iterator. |
| 13 | /// |
| 14 | /// During the execution of `exactly_one` the iterator must be mutated. This wrapper |
| 15 | /// effectively "restores" the state of the input iterator when it's handed back. |
| 16 | /// |
| 17 | /// This is very similar to `PutBackN` except this iterator only supports 0-2 elements and does not |
| 18 | /// use a `Vec`. |
| 19 | #[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:)] |
| 20 | pub struct ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 21 | where |
| 22 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 23 | { |
| 24 | first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eitherrust-analyzer cargo either 1.15.0 Either#<[I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; 2], I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>>, |
| 25 | innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner.: I, |
| 26 | } |
| 27 | |
| 28 | impl<I> ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 29 | where |
| 30 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 31 | { |
| 32 | /// Creates a new `ExactlyOneErr` iterator. |
| 33 | pub(craterust-analyzer cargo itertools 0.14.0 crate/) fn newrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`]new().(first_two: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eitherrust-analyzer cargo either 1.15.0 Either#<[I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; 2], I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>>, inner: I) -> Selfrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`] { |
| 34 | Selfrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`] { first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two., innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner. } |
| 35 | } |
| 36 | |
| 37 | fn additional_lenrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`]additional_len().(&self) -> usize { |
| 38 | match self.first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two. { |
| 39 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Leftrust-analyzer cargo either 1.15.0 Either#Left#(_)) => 2, |
| 40 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Rightrust-analyzer cargo either 1.15.0 Either#Right#(_)) => 1, |
| 41 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => 0, |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 47 | where |
| 48 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 49 | { |
| 50 | type Itemrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 51 | |
| 52 | fn nextrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<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 exactly_one_err/impl#[`ExactlyOneError<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 53 | match self.first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() { |
| 54 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Leftrust-analyzer cargo either 1.15.0 Either#Left#([first, second])) => { |
| 55 | self.first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Rightrust-analyzer cargo either 1.15.0 Either#Right#(second)); |
| 56 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first) |
| 57 | } |
| 58 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Rightrust-analyzer cargo either 1.15.0 Either#Right#(second)) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(second), |
| 59 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => self.innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().(), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn size_hintrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 64 | size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::add_scalarrust-analyzer cargo itertools 0.14.0 size_hint/add_scalar().(self.innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner..size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().(), self.additional_lenrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`]additional_len().()) |
| 65 | } |
| 66 | |
| 67 | fn foldrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Iterator]fold().<B, F>(self, mut init: B, mut f: F) -> B |
| 68 | where |
| 69 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(B, Selfrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> B, |
| 70 | { |
| 71 | match self.first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two. { |
| 72 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Leftrust-analyzer cargo either 1.15.0 Either#Left#([first, second])) => { |
| 73 | init = f(init, first); |
| 74 | init = f(init, second); |
| 75 | } |
| 76 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Rightrust-analyzer cargo either 1.15.0 Either#Right#(second)) => init = f(init, second), |
| 77 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {} |
| 78 | } |
| 79 | self.innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner..foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#fold().(init, f) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl<I> ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# for ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> where I: ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator# {} |
| 84 | |
| 85 | impl<I> Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 86 | where |
| 87 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 88 | { |
| 89 | fn fmtrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> FmtResultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 90 | let additional = self.additional_lenrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`]additional_len().(); |
| 91 | if additional rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 92 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "got at least 2 elements when exactly one was expected") |
| 93 | } else { |
| 94 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "got zero elements when exactly one was expected") |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl<I> Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 100 | where |
| 101 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 102 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 103 | { |
| 104 | fn fmtrust-analyzer cargo itertools 0.14.0 exactly_one_err/impl#[`ExactlyOneError<I>`][Debug]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> FmtResultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 105 | let mut dbg = f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("ExactlyOneError"); |
| 106 | match &self.first_tworust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#first_two. { |
| 107 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Leftrust-analyzer cargo either 1.15.0 Either#Left#([first, second])) => { |
| 108 | dbg.fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("first", first).fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("second", second); |
| 109 | } |
| 110 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eitherrust-analyzer cargo either 1.15.0 Either#::Rightrust-analyzer cargo either 1.15.0 Either#Right#(second)) => { |
| 111 | dbg.fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("second", second); |
| 112 | } |
| 113 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {} |
| 114 | } |
| 115 | dbg.fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("inner", &self.innerrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#inner.).finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #[cfg(feature = "use_std")] |
| 120 | impl<I> Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for ExactlyOneErrorrust-analyzer cargo itertools 0.14.0 exactly_one_err/ExactlyOneError#<I> |
| 121 | where |
| 122 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 123 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 124 | { |
| 125 | } |