| 1 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#; |
| 3 | 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/::{FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#, IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#}; |
| 4 | |
| 5 | /// A wrapper for `Rc<RefCell<I>>`, that implements the `Iterator` trait. |
| 6 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 7 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 8 | pub struct RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> { |
| 9 | /// The boxed iterator. |
| 10 | pub rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter.: Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<I>>, |
| 11 | } |
| 12 | |
| 13 | /// Return an iterator inside a `Rc<RefCell<_>>` wrapper. |
| 14 | /// |
| 15 | /// The returned `RcIter` can be cloned, and each clone will refer back to the |
| 16 | /// same original iterator. |
| 17 | /// |
| 18 | /// `RcIter` allows doing interesting things like using `.zip()` on an iterator with |
| 19 | /// itself, at the cost of runtime borrow checking which may have a performance |
| 20 | /// penalty. |
| 21 | /// |
| 22 | /// Iterator element type is `Self::Item`. |
| 23 | /// |
| 24 | /// ``` |
| 25 | /// use itertools::rciter; |
| 26 | /// use itertools::zip; |
| 27 | /// |
| 28 | /// // In this example a range iterator is created and we iterate it using |
| 29 | /// // three separate handles (two of them given to zip). |
| 30 | /// // We also use the IntoIterator implementation for `&RcIter`. |
| 31 | /// |
| 32 | /// let mut iter = rciter(0..9); |
| 33 | /// let mut z = zip(&iter, &iter); |
| 34 | /// |
| 35 | /// assert_eq!(z.next(), Some((0, 1))); |
| 36 | /// assert_eq!(z.next(), Some((2, 3))); |
| 37 | /// assert_eq!(z.next(), Some((4, 5))); |
| 38 | /// assert_eq!(iter.next(), Some(6)); |
| 39 | /// assert_eq!(z.next(), Some((7, 8))); |
| 40 | /// assert_eq!(z.next(), None); |
| 41 | /// ``` |
| 42 | /// |
| 43 | /// **Panics** in iterator methods if a borrow error is encountered in the |
| 44 | /// iterator methods. It can only happen if the `RcIter` is reentered in |
| 45 | /// `.next()`, i.e. if it somehow participates in an “iterator knot” |
| 46 | /// where it is an adaptor of itself. |
| 47 | pub fn rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/rciter().<I>(iterable: I) -> RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#> |
| 48 | where |
| 49 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 50 | { |
| 51 | RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter# { |
| 52 | rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter.: Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().(RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(iterable.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().())), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl<I> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> { |
| 57 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter.); |
| 58 | } |
| 59 | |
| 60 | impl<A, I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> |
| 61 | where |
| 62 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = A>, |
| 63 | { |
| 64 | type Itemrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<I>`][Iterator]Item# = A; |
| 65 | #[inline] |
| 66 | fn nextrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<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 rciter_impl/impl#[`RcIter<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 67 | self.rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() |
| 68 | } |
| 69 | |
| 70 | #[inline] |
| 71 | fn size_hintrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 72 | // To work sanely with other API that assume they own an iterator, |
| 73 | // so it can't change in other places, we can't guarantee as much |
| 74 | // in our size_hint. Other clones may drain values under our feet. |
| 75 | (0, self.rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().().1) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl<I> DoubleEndedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/double_ended/DoubleEndedIterator# for RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> |
| 80 | where |
| 81 | I: DoubleEndedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/double_ended/DoubleEndedIterator#, |
| 82 | { |
| 83 | #[inline] |
| 84 | fn next_backrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<I>`][DoubleEndedIterator]next_back().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<I>`][DoubleEndedIterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 85 | self.rciterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#rciter..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().next_backrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/double_ended/DoubleEndedIterator#next_back().() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Return an iterator from `&RcIter<I>` (by simply cloning it). |
| 90 | impl<I> IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator# for &RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> |
| 91 | where |
| 92 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 93 | { |
| 94 | type Itemrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`&RcIter<I>`][IntoIterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 95 | type IntoIterrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`&RcIter<I>`][IntoIterator]IntoIter# = RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I>; |
| 96 | |
| 97 | fn into_iterrust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`&RcIter<I>`][IntoIterator]into_iter().(self) -> RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> { |
| 98 | self.clonerust-analyzer cargo itertools 0.14.0 rciter_impl/impl#[`RcIter<I>`][Clone]clone().() |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl<A, I> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for RcIterrust-analyzer cargo itertools 0.14.0 rciter_impl/RcIter#<I> where I: FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = A> {} |