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().
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>
impl<I, T> !Sync for IterState<I, T>
impl<I, T> Unpin for IterState<I, T>
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> 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> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
type CastFrom = CastSizedExact
Source§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 moreimpl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> InvariantsEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> InvariantsEq<T> for Twhere
T: ?Sized,
impl<T> InvariantsEq<Unalign<T>> for T
impl<T> InvariantsEq<Wrapping<T>> for T
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseInvariantsEq> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: TransmuteFrom<Src, SV, DV> + InvariantsEq<Src> + ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Cell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ManuallyDrop<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ReadOnly<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
impl<T> TransmuteFrom<Unalign<T>, Valid, Valid> for T
impl<T> TransmuteFrom<UnsafeCell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Wrapping<T>, Valid, Valid> for T
impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
C: CastExact<Src, Dst>,
Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized,
Src: ?Sized,
impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized,
C: CastExact<Src, Dst>,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.