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 beExactSizeIteratoror 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
Dataptror serialization - Safe: Uses
RefCellfor interior mutability, protected by R’s GC
Fields§
§len: usizeVector 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>,
impl<I, T> IterState<I, T>where
I: Iterator<Item = T>,
Sourcepub fn new(iter: I, len: usize) -> Self
pub fn new(iter: I, len: usize) -> Self
Create a new iterator state with an explicit length.
§Arguments
iter: The iterator to wraplen: 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.
Sourcepub fn get_element(&self, i: usize) -> Option<T>where
T: Copy,
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 generatedNoneif index is out of bounds or iterator exhausted before reaching indexi
Sourcepub fn materialize_all(&self) -> &[T]
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.
Sourcepub fn as_materialized(&self) -> Option<&[T]>
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§impl<I, T> IterState<I, T>where
I: ExactSizeIterator<Item = T>,
impl<I, T> IterState<I, T>where
I: ExactSizeIterator<Item = T>,
Sourcepub fn from_exact_size(iter: I) -> Self
pub fn from_exact_size(iter: I) -> Self
Create a new iterator state from an ExactSizeIterator.
The length is automatically determined from iter.len().