Skip to main content

luaur_common/records/
iterator.rs

1//! `DenseHashTable::iterator` — the mutable counterpart of `const_iterator`,
2//! yielding `&mut Item` over occupied slots. Reference:
3//! `luau/Common/include/Luau/DenseHash.h` (the `iterator` nested type).
4//!
5//! Unlike the const iterator it walks a `slice::IterMut` directly (a shared
6//! borrow of the whole table would conflict with the `&mut Item` it yields), so
7//! it carries its own copy of the `empty_key` sentinel and `eq` functor to skip
8//! free slots.
9
10use core::marker::PhantomData;
11
12use crate::records::dense_hash_table::{DenseEq, ItemInterface};
13
14pub struct MutIterator<'a, K, I, Iface, E> {
15    pub(crate) inner: core::slice::IterMut<'a, I>,
16    pub(crate) empty_key: K,
17    pub(crate) eq: E,
18    pub(crate) _iface: PhantomData<Iface>,
19}
20
21impl<'a, K, I, Iface, E> Iterator for MutIterator<'a, K, I, Iface, E>
22where
23    Iface: ItemInterface<K, I>,
24    E: DenseEq<K>,
25{
26    type Item = &'a mut I;
27
28    fn next(&mut self) -> Option<&'a mut I> {
29        for item in self.inner.by_ref() {
30            if !self.eq.eq(Iface::get_key(item), &self.empty_key) {
31                return Some(item);
32            }
33        }
34        None
35    }
36}