Reiterator

Struct Reiterator 

Source
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: usize

Safe 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>

Source

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.

Source

pub fn restart(&mut self)

Set the index to zero. Literal drop-in equivalent for .index = 0, always inlined. Clearer, I guess.

Source

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.

Source

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.

Source

pub fn lazy_next(&mut self) -> Option<usize>

Advance the index without computing the corresponding value.

Source

pub fn next(&mut self) -> Option<Indexed<'_, I::Item>>

Like Iterator::next but with a dependent lifetime.

Source

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.

Source

pub fn map_indices<UnReferenceInator: FnMut(usize) -> Output, Output>( self, un_reference_inator: UnReferenceInator, ) -> MapIndices<I, UnReferenceInator, Output>

Map indices to a known lifetime.

Source

pub fn map_values<UnReferenceInator: FnMut(&I::Item) -> Output, Output>( self, un_reference_inator: UnReferenceInator, ) -> MapValues<I, UnReferenceInator, Output>

Map values to a known lifetime.

Source

pub fn cloned( self, ) -> Map<I, impl FnMut(Indexed<'_, I::Item>) -> (usize, I::Item), (usize, I::Item)>
where I::Item: Clone,

Clone values lazily as we produce them.

Auto Trait Implementations§

§

impl<I> Freeze for Reiterator<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for Reiterator<I>

§

impl<I> Send for Reiterator<I>
where I: Send, <I as Iterator>::Item: Send,

§

impl<I> Sync for Reiterator<I>
where I: Sync, <I as Iterator>::Item: Sync,

§

impl<I> Unpin for Reiterator<I>
where I: Unpin, <I as Iterator>::Item: Unpin,

§

impl<I> UnwindSafe for Reiterator<I>
where I: UnwindSafe, <I as Iterator>::Item: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.