Skip to main content

IterState

Struct IterState 

Source
pub struct IterState<I, T> {
    len: usize,
    iter: RefCell<Option<I>>,
    cache: RefCell<Vec<T>>,
    materialized: OnceLock<Vec<T>>,
}
Expand description

Core state for iterator-backed ALTREP vectors.

Provides lazy element generation with caching for random-access semantics. Iterator elements are cached as they’re accessed, enabling repeatable reads.

§Type Parameters

  • I: The iterator type (must be ExactSizeIterator or provide explicit length)
  • T: The element type produced by the iterator

§Design

  • Lazy: Elements generated on-demand via elt(i)
  • Cached: Once generated, elements stored in cache for repeat access
  • Materializable: Can be fully materialized for Dataptr or serialization
  • Safe: Uses RefCell for interior mutability, protected by R’s GC

Fields§

§len: usize

Vector length (from ExactSizeIterator::len() or explicit)

§iter: RefCell<Option<I>>

Iterator state (consumed as we advance)

§cache: RefCell<Vec<T>>

Cache of generated elements (prefix of the vector)

§materialized: OnceLock<Vec<T>>

Full materialization (when all elements have been generated)

Implementations§

Source§

impl<I, T> IterState<I, T>
where I: Iterator<Item = T>,

Source

pub fn new(iter: I, len: usize) -> Self

Create a new iterator state with an explicit length.

§Arguments
  • iter: The iterator to wrap
  • len: The expected number of elements
§Length Mismatch

If the iterator produces a different number of elements than len:

  • Fewer elements: Missing indices return None/NA/default values
  • More elements: Extra elements are ignored (truncated to len)

A warning is printed to stderr when a mismatch is detected.

Source

pub fn get_element(&self, i: usize) -> Option<T>
where T: Copy,

Ensure the element at index i is in the cache and return it by value.

Advances the iterator as needed. Only works for Copy types.

§Returns
  • Some(T) if element exists and has been generated
  • None if index is out of bounds or iterator exhausted before reaching index i
Source

pub fn materialize_all(&self) -> &[T]

Materialize all remaining elements from the iterator.

After this call, all elements are guaranteed to be in memory and as_materialized() will return Some.

§Length Mismatch Handling

If the iterator produces fewer elements than declared len, the missing elements are left uninitialized in the cache (callers should handle this via bounds checking). If the iterator produces more elements than declared, extra elements are silently ignored (truncated to len).

A warning is printed to stderr if a length mismatch is detected.

Source

pub fn as_materialized(&self) -> Option<&[T]>

Get the materialized vector if all elements have been generated.

Returns None if not yet fully materialized.

Source

pub fn len(&self) -> usize

Get the current length.

Source

pub fn is_empty(&self) -> bool

Check if the vector is empty.

Source§

impl<I, T> IterState<I, T>
where I: ExactSizeIterator<Item = T>,

Source

pub fn from_exact_size(iter: I) -> Self

Create a new iterator state from an ExactSizeIterator.

The length is automatically determined from iter.len().

Auto Trait Implementations§

§

impl<I, T> !Freeze for IterState<I, T>

§

impl<I, T> !RefUnwindSafe for IterState<I, T>

§

impl<I, T> Send for IterState<I, T>
where I: Send, T: Send,

§

impl<I, T> !Sync for IterState<I, T>

§

impl<I, T> Unpin for IterState<I, T>
where I: Unpin, T: Unpin,

§

impl<I, T> UnsafeUnpin for IterState<I, T>
where I: UnsafeUnpin,

§

impl<I, T> UnwindSafe for IterState<I, T>
where I: UnwindSafe, T: 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.