| 1 | //! Iterators that are sources (produce elements from parameters, |
| 2 | //! not from another iterator). |
| 3 | #![allow(deprecated)] |
| 4 | |
| 5 | 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/; |
| 6 | use 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/; |
| 7 | |
| 8 | /// Creates a new unfold source with the specified closure as the "iterator |
| 9 | /// function" and an initial state to eventually pass to the closure |
| 10 | /// |
| 11 | /// `unfold` is a general iterator builder: it has a mutable state value, |
| 12 | /// and a closure with access to the state that produces the next value. |
| 13 | /// |
| 14 | /// This more or less equivalent to a regular struct with an [`Iterator`] |
| 15 | /// implementation, and is useful for one-off iterators. |
| 16 | /// |
| 17 | /// ``` |
| 18 | /// // an iterator that yields sequential Fibonacci numbers, |
| 19 | /// // and stops at the maximum representable value. |
| 20 | /// |
| 21 | /// use itertools::unfold; |
| 22 | /// |
| 23 | /// let mut fibonacci = unfold((1u32, 1u32), |(x1, x2)| { |
| 24 | /// // Attempt to get the next Fibonacci number |
| 25 | /// let next = x1.saturating_add(*x2); |
| 26 | /// |
| 27 | /// // Shift left: ret <- x1 <- x2 <- next |
| 28 | /// let ret = *x1; |
| 29 | /// *x1 = *x2; |
| 30 | /// *x2 = next; |
| 31 | /// |
| 32 | /// // If addition has saturated at the maximum, we are finished |
| 33 | /// if ret == *x1 && ret > 1 { |
| 34 | /// None |
| 35 | /// } else { |
| 36 | /// Some(ret) |
| 37 | /// } |
| 38 | /// }); |
| 39 | /// |
| 40 | /// itertools::assert_equal(fibonacci.by_ref().take(8), |
| 41 | /// vec![1, 1, 2, 3, 5, 8, 13, 21]); |
| 42 | /// assert_eq!(fibonacci.last(), Some(2_971_215_073)) |
| 43 | /// ``` |
| 44 | #[deprecated( |
| 45 | note = "Use [std::iter::from_fn](https://doc.rust-lang.org/std/iter/fn.from_fn.html) instead", |
| 46 | since = "0.13.0" |
| 47 | )] |
| 48 | pub fn unfoldrust-analyzer cargo itertools 0.14.0 sources/unfold().<A, St, F>(initial_state: St, f: F) -> Unfoldrust-analyzer cargo itertools 0.14.0 sources/Unfold#<St, F> |
| 49 | where |
| 50 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut St) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<A>, |
| 51 | { |
| 52 | Unfoldrust-analyzer cargo itertools 0.14.0 sources/Unfold# { |
| 53 | frust-analyzer cargo itertools 0.14.0 sources/Unfold#f., |
| 54 | staterust-analyzer cargo itertools 0.14.0 sources/Unfold#state.: initial_state, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl<St, F> 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 Unfoldrust-analyzer cargo itertools 0.14.0 sources/Unfold#<St, F> |
| 59 | where |
| 60 | St: 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#, |
| 61 | { |
| 62 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(Unfold, staterust-analyzer cargo itertools 0.14.0 sources/Unfold#state.); |
| 63 | } |
| 64 | |
| 65 | /// See [`unfold`](crate::unfold) for more information. |
| 66 | #[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:)] |
| 67 | #[must_use = "iterators are lazy and do nothing unless consumed"] |
| 68 | #[deprecated( |
| 69 | note = "Use [std::iter::FromFn](https://doc.rust-lang.org/std/iter/struct.FromFn.html) instead", |
| 70 | since = "0.13.0" |
| 71 | )] |
| 72 | pub struct Unfoldrust-analyzer cargo itertools 0.14.0 sources/Unfold#<St, F> { |
| 73 | frust-analyzer cargo itertools 0.14.0 sources/Unfold#f.: F, |
| 74 | /// Internal state that will be passed to the closure on the next iteration |
| 75 | pub staterust-analyzer cargo itertools 0.14.0 sources/Unfold#state.: St, |
| 76 | } |
| 77 | |
| 78 | impl<A, St, F> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Unfoldrust-analyzer cargo itertools 0.14.0 sources/Unfold#<St, F> |
| 79 | where |
| 80 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut St) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<A>, |
| 81 | { |
| 82 | type Itemrust-analyzer cargo itertools 0.14.0 sources/impl#[`Unfold<St, F>`][Iterator]Item# = A; |
| 83 | |
| 84 | #[inline] |
| 85 | fn nextrust-analyzer cargo itertools 0.14.0 sources/impl#[`Unfold<St, F>`][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 sources/impl#[`Unfold<St, F>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 86 | (self.frust-analyzer cargo itertools 0.14.0 sources/Unfold#f.)(&mut self.staterust-analyzer cargo itertools 0.14.0 sources/Unfold#state.) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// An iterator that infinitely applies function to value and yields results. |
| 91 | /// |
| 92 | /// This `struct` is created by the [`iterate()`](crate::iterate) function. |
| 93 | /// See its documentation for more. |
| 94 | #[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:)] |
| 95 | #[must_use = "iterators are lazy and do nothing unless consumed"] |
| 96 | pub struct Iteraterust-analyzer cargo itertools 0.14.0 sources/Iterate#<St, F> { |
| 97 | staterust-analyzer cargo itertools 0.14.0 sources/Iterate#state.: St, |
| 98 | frust-analyzer cargo itertools 0.14.0 sources/Iterate#f.: F, |
| 99 | } |
| 100 | |
| 101 | impl<St, F> 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 Iteraterust-analyzer cargo itertools 0.14.0 sources/Iterate#<St, F> |
| 102 | where |
| 103 | St: 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#, |
| 104 | { |
| 105 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(Iterate, staterust-analyzer cargo itertools 0.14.0 sources/Iterate#state.); |
| 106 | } |
| 107 | |
| 108 | impl<St, F> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Iteraterust-analyzer cargo itertools 0.14.0 sources/Iterate#<St, F> |
| 109 | where |
| 110 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&St) -> St, |
| 111 | { |
| 112 | type Itemrust-analyzer cargo itertools 0.14.0 sources/impl#[`Iterate<St, F>`][Iterator]Item# = St; |
| 113 | |
| 114 | #[inline] |
| 115 | fn nextrust-analyzer cargo itertools 0.14.0 sources/impl#[`Iterate<St, F>`][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 sources/impl#[`Iterate<St, F>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 116 | let next_state = (self.frust-analyzer cargo itertools 0.14.0 sources/Iterate#f.)(&self.staterust-analyzer cargo itertools 0.14.0 sources/Iterate#state.); |
| 117 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(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 self.staterust-analyzer cargo itertools 0.14.0 sources/Iterate#state., next_state)) |
| 118 | } |
| 119 | |
| 120 | #[inline] |
| 121 | fn size_hintrust-analyzer cargo itertools 0.14.0 sources/impl#[`Iterate<St, F>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 122 | (usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// Creates a new iterator that infinitely applies function to value and yields results. |
| 127 | /// |
| 128 | /// ``` |
| 129 | /// use itertools::iterate; |
| 130 | /// |
| 131 | /// itertools::assert_equal(iterate(1, |i| i % 3 + 1).take(5), vec![1, 2, 3, 1, 2]); |
| 132 | /// ``` |
| 133 | /// |
| 134 | /// **Panics** if compute the next value does. |
| 135 | /// |
| 136 | /// ```should_panic |
| 137 | /// # use itertools::iterate; |
| 138 | /// let mut it = iterate(25u32, |x| x - 10).take_while(|&x| x > 10); |
| 139 | /// assert_eq!(it.next(), Some(25)); // `Iterate` holds 15. |
| 140 | /// assert_eq!(it.next(), Some(15)); // `Iterate` holds 5. |
| 141 | /// it.next(); // `5 - 10` overflows. |
| 142 | /// ``` |
| 143 | /// |
| 144 | /// You can alternatively use [`core::iter::successors`] as it better describes a finite iterator. |
| 145 | pub fn iteraterust-analyzer cargo itertools 0.14.0 sources/iterate().<St, F>(initial_value: St, f: F) -> Iteraterust-analyzer cargo itertools 0.14.0 sources/Iterate#<St, F> |
| 146 | where |
| 147 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&St) -> St, |
| 148 | { |
| 149 | Iteraterust-analyzer cargo itertools 0.14.0 sources/Iterate# { |
| 150 | staterust-analyzer cargo itertools 0.14.0 sources/Iterate#state.: initial_value, |
| 151 | frust-analyzer cargo itertools 0.14.0 sources/Iterate#f., |
| 152 | } |
| 153 | } |