/
nv-lang
/
nova
Обзор
Документация
Войти
/
nv-lang
/
nova
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
std/src/collections/vec/iter.nv
54 строки
2 KB
Evgeniy Golovin
fix(221.1): №254 — bound-check + specificity для Next[T]/Iter[I], Iter-делегаты, T-binding, разворот 16 обходов
03 авг 2026, 14:37
03 авг 2026, 14:37
ef341bd
Код
Авторство
О чём код?
// std.collections.vec (iter) — `VecIter[T]` index-cursor iterator. // // `@iter()` powers `for x in v` (Iter[T] protocol) and is the source for // the eager combinators in `functional.nv`. The lazy adapter family // (map/filter/… → collect) builds on this `Next`/`Iter` foundation. #prelude(core, runtime, collections, protocols) module collections.vec // ─── Iterable[T] ───────────────────────────────────────────────────────── /// Index-cursor iterator over a `Vec[T]`. /// /// Holds a copy of the buffer pointer (`data`) and the element count at the /// moment `iter()` was called; the GC sees the data pointer through this /// record and keeps the buffer alive. Concurrent mutation of the originating /// `Vec` during iteration is *not* checked — the caller is responsible for /// not pushing/removing while iterating. export type VecIter[T] value { priv data *mut T priv mut idx int priv len int } /// Return an iterator that yields each element in insertion order. /// /// # Examples /// /// ```nova /// let v = Vec[int].of(10, 20, 30) /// for x in v { println(x) } // 10, 20, 30 /// ``` #impl(Iter[VecIter[T]]) export fn Vec[T] @iter() -> VecIter[T] => { @data, idx: 0, @len } /// Advance the iterator. Returns `Some(T)` while elements remain, `None` /// once the vector is exhausted (subsequent calls keep returning `None`). #impl(Next[T]) export fn VecIter[T] mut @next() -> Option[T] { if @idx >= @len { return None } ro v = unsafe { @data.read_at(@idx) } @idx += 1 Some(v) } /// Shallow copy — an independent cursor over the SAME underlying buffer /// (does NOT deep-copy the vector data; `data`/`len` are shared, only `idx` /// is per-clone). D246 amendment ([M-ro-launder-via-mut-binding], Ф.2 /// migration, 2026-07-23): sanctioned `.clone()` door for a caller that needs /// a private, independently-advancing cursor WITHOUT aliasing the original /// (in-out via `mut`-param would be WRONG here — the whole point is that /// the clone must NOT propagate its advancement back to the source; see /// `vec_lazy.box_iter`, which seeds a deferred-iteration closure). export fn VecIter[T] @clone() -> VecIter[T] => { @data, @idx, @len }