Module collections/list_view

collections/list_view

ListView — a SAFE window over an ArrayList(T).

The safe replacement for borrowed slices (plans/SLICE_REWORK.md §3): a ListView holds the Rc'd backing list and indexes through the LIVE handle. By construction it cannot dangle:

  • the view's Rc keeps the backing object alive (reassigning the original variable doesn't matter),
  • every access re-reads the CURRENT buffer through the handle, so realloc-on-growth cannot leave a stale pointer,
  • shrinkage is caught by bounds checks (a clean .None, never UB),
  • alias mutation is VISIBLE (alias semantics, not a snapshot).

Cost model: copying a view = one Rc incref; access = one extra indirection + bounds check. No copy-on-write.

Types

ListView type-function
fn(comptime(T) : Type) -> (comptime(fn_return_yocaca937f_id_18) : Type)

Type Parameters

NameTypeNotes
TTypecomptime
impl(forall(T : Type), ListView(T), ...)
new : (fn(backing : ArrayList(T), offset : usize, len : usize) -> Self)

Create a view of [offset, offset+len) over backing. The window is clamped to the backing's CURRENT length.

Returns: Self

of : (fn(backing : ArrayList(T)) -> Self)

View of the whole list.

Returns: Self

len : (fn(self : Self) -> usize)

The view's length (fixed at construction; may exceed what the backing currently holds if it shrank — reads past it yield .None).

Returns: usize

is_empty : (fn(self : Self) -> bool)

Returns: bool

get : (fn(self : Self, i : usize) -> Option(T))

Read element i THROUGH the live backing. Bounds-checked against both the view window and the backing's current length.

Returns: Option(T)

view : (fn(self : Self, sub_offset : usize, sub_len : usize) -> Self)

A sub-view of this view (still aliasing the same backing). (Params avoid the names of sibling methods bound earlier in this impl — e.g. len — which cannot be shadowed.)

Returns: Self

to_list : (fn(self : Self) -> ArrayList(T))

Owned copy of the window's CURRENT contents.

Returns: ArrayList(T)