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
  • Borrow-checked: Uses RefCell for interior mutability on the R thread

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

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.

Source

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.

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

Source

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.

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().

Trait Implementations§

Source§

impl<I: Clone, T: Clone> Clone for IterState<I, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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>
where RefCell<Option<I>>: Send, RefCell<Vec<T>>: Send, OnceLock<Vec<T>>: Send,

§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ConsumingFallible for T
where T: Clone,

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> RClone for T
where T: Clone,

Source§

fn r_clone(&self) -> T

Create a deep copy of this value.
Source§

#[doc(hidden)]
unsafe extern "C" fn __RClone_r_clone_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RClone + IntoR,

Method shim for RClone::r_clone. Read more
Source§

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

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.

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.