pub struct Reiterator<I: Iterator> {
pub index: usize,
/* private fields */
}Expand description
Caching repeatable iterator that only ever calculates each element once. NOTE that if the iterator is not referentially transparent (i.e. pure, e.g. mutable state), this will not necessarily work! We replace a call to a previously evaluated index with the value we already made, so side effects will not show up at all.
Fields§
§index: usizeSafe to edit! Assign any value, even out of bounds, and nothing will break:
- If the index is in bounds, the next time you call
get/next, we calculate each element until this one (if not already cached). - If the index is out of bounds, we return
None(after exhausting the iterator: it’s not necessarily a fixed size, so there’s only one way to find out). Note that this iterator is lazy, so assigning an index doesn’t mean that the value at that index has been calculated.
Implementations§
Source§impl<I: Iterator> Reiterator<I>
impl<I: Iterator> Reiterator<I>
Sourcepub fn new<II: IntoIterator<IntoIter = I>>(into_iter: II) -> Self
pub fn new<II: IntoIterator<IntoIter = I>>(into_iter: II) -> Self
Set up the iterator to return the first element, but don’t calculate it yet.
Sourcepub fn restart(&mut self)
pub fn restart(&mut self)
Set the index to zero. Literal drop-in equivalent for .index = 0, always inlined. Clearer, I guess.
Sourcepub fn at(&mut self, index: usize) -> Option<&I::Item>
pub fn at(&mut self, index: usize) -> Option<&I::Item>
Return the element at the requested index or compute it if we haven’t, provided it’s in bounds.
Sourcepub fn get(&mut self) -> Option<Indexed<'_, I::Item>>
pub fn get(&mut self) -> Option<Indexed<'_, I::Item>>
Return the current element or compute it if we haven’t, provided it’s in bounds.
This can be called any number of times in a row to return the exact same item;
we won’t advance to the next element until you explicitly call next.
Sourcepub fn lazy_next(&mut self) -> Option<usize>
pub fn lazy_next(&mut self) -> Option<usize>
Advance the index without computing the corresponding value.
Sourcepub fn next(&mut self) -> Option<Indexed<'_, I::Item>>
pub fn next(&mut self) -> Option<Indexed<'_, I::Item>>
Like Iterator::next but with a dependent lifetime.
Sourcepub fn map<UnReferenceInator: FnMut(Indexed<'_, I::Item>) -> Output, Output>(
self,
un_reference_inator: UnReferenceInator,
) -> Map<I, UnReferenceInator, Output> ⓘ
pub fn map<UnReferenceInator: FnMut(Indexed<'_, I::Item>) -> Output, Output>( self, un_reference_inator: UnReferenceInator, ) -> Map<I, UnReferenceInator, Output> ⓘ
Map Indexeds to a known lifetime.
Sourcepub fn map_indices<UnReferenceInator: FnMut(usize) -> Output, Output>(
self,
un_reference_inator: UnReferenceInator,
) -> MapIndices<I, UnReferenceInator, Output> ⓘ
pub fn map_indices<UnReferenceInator: FnMut(usize) -> Output, Output>( self, un_reference_inator: UnReferenceInator, ) -> MapIndices<I, UnReferenceInator, Output> ⓘ
Map indices to a known lifetime.