Skip to main content

ListContext

Struct ListContext 

Source
pub struct ListContext {
    pub index: Mutex<usize>,
    pub elements: Arc<[Arc<Thunk>]>,
    pub iter_binding: Mutex<Option<(Arc<str>, Value)>>,
}
Expand description

Iteration context for &prev / &next / &index references inside a list.

Serves two scenarios:

  1. Plain list-literal iteration (&prev / &next / &index) — built by Scope::with_list_context. Each element runs under its own child scope with a fixed index; iter_binding is always None.
  2. Comprehension hot loop (for x in xs) — built once by Scope::with_iter_loop, then the body reuses the same Arc<Scope> across all iterations. Scope::set_iter_binding refreshes iter_binding + index in place per element, eliminating the Arc::new(Self {...}) per element flagged by the P1-B diagnostic correction (48 MB / 200 K blocks). iter_binding is Mutex-wrapped so Scope stays Send + Sync without unsafe interior mutability.

P2-1: thunks shared via Arc<[Arc<Thunk>]> so Expr::List’s per-element with_list_context(i, elements) only bumps an Arc instead of cloning the whole Vec<Arc<Thunk>> (O(N²) → O(N)).

Fields§

§index: Mutex<usize>§elements: Arc<[Arc<Thunk>]>

P2-1: Arc<[Arc<Thunk>]> lets with_list_context share the thunks slice across every element’s scope via a refcount bump, instead of Vec::clone’ing all N entries per element (the prior shape paid O(N²) Arc bumps over an N-element list).

§iter_binding: Mutex<Option<(Arc<str>, Value)>>

Current named-iteration binding (name, value). None for plain list iteration. Comprehension hot loops fill this in once per element via a lock + write (no allocation, no frame rebuild).

Semantic invariant: when a Closure value is constructed inside a loop body, the constructor MUST snapshot the current binding into captured_env (see the Expr::Closure branch in eval.rs). Otherwise a later iteration would mutate the binding the closure was meant to capture, breaking lexical-snapshot semantics.

Implementations§

Source§

impl ListContext

Source

pub fn fixed(index: usize, elements: Arc<[Arc<Thunk>]>) -> Self

Plain &prev / &next / &index scenario: fixed index, no iter binding.

Source

pub fn empty(elements: Arc<[Arc<Thunk>]>) -> Self

Comprehension hot loop entry: starts at index 0 with an empty binding slot. Scope::set_iter_binding fills it per element.

Source

pub fn current_index(&self) -> usize

Read the current iteration index — used by &index / &prev / &next.

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.