Skip to main content

luaur_common/records/
const_iterator.rs

1//! `DenseHashTable::const_iterator` — a forward iterator over occupied slots,
2//! yielding `&Item`. Reference: `luau/Common/include/Luau/DenseHash.h` (the
3//! `const_iterator` nested type). The map/set wrappers seed it with
4//! `first_occupied()` and adapt the yielded `&Item` into `&Key`/`(&Key, &Value)`.
5
6use crate::records::dense_hash_table::{DenseEq, DenseHashTable, DenseHasher, ItemInterface};
7
8pub struct ConstIterator<'a, K, I, Iface, H, E> {
9    pub(crate) table: &'a DenseHashTable<K, I, Iface, H, E>,
10    pub(crate) index: usize,
11}
12
13impl<'a, K, I, Iface, H, E> Iterator for ConstIterator<'a, K, I, Iface, H, E>
14where
15    K: Clone,
16    Iface: ItemInterface<K, I>,
17    H: DenseHasher<K> + Default,
18    E: DenseEq<K> + Default,
19{
20    type Item = &'a I;
21
22    fn next(&mut self) -> Option<&'a I> {
23        if self.index >= self.table.capacity {
24            return None;
25        }
26        let item = &self.table.data[self.index];
27        self.index = self.table.next_occupied(self.index);
28        Some(item)
29    }
30}