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 - Borrow-checked: Uses
RefCellfor interior mutability on the R thread
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
get_element returns None for a missing index.
materialize_all requires the complete declared
prefix and panics if the source ends early. Neither reads past len.
Sourcepub fn from_values(values: Vec<T>) -> Self
pub fn from_values(values: Vec<T>) -> Self
Restore a realized sequence without constructing or advancing an iterator.
The iterator type is retained, but its state is absent: all reads use the
supplied values. This supports deserialization even when I: !Default.
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 exactly the declared prefix, including already cached elements.
Does not request an element beyond len, including when len is zero
or the source is infinite. On success, Self::as_materialized returns
a slice whose length is exactly Self::len.
§Panics
If the source ends before the declared length. The partial prefix stays cached, but is never published as a complete buffer. A failed attempt drops the iterator; another attempt cannot resume an exhausted source.
Sourcepub fn try_materialize_all(&self) -> Result<&[T], usize>
pub fn try_materialize_all(&self) -> Result<&[T], usize>
Self::materialize_all without the panic: Err carries the number
of elements the source produced before it ended.
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().
Trait Implementations§
Auto Trait Implementations§
impl<I, T> !Freeze for IterState<I, T>
impl<I, T> !RefUnwindSafe for IterState<I, T>
impl<I, T> !Sync for IterState<I, T>
impl<I, T> Send for IterState<I, T>
impl<I, T> Unpin for IterState<I, T>
impl<I, T> UnsafeUnpin for IterState<I, T>
impl<I, T> UnwindSafe for IterState<I, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConsumingFallible for Twhere
T: Clone,
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreLayout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.