Skip to main content

mgui_core/
arena.rs

1use slotmap::SlotMap;
2
3/// 阶段 1 直接复用 slotmap,后续如需自定义 generational index 再替换。
4pub struct Arena<K: slotmap::Key, V> {
5    inner: SlotMap<K, V>,
6}
7
8impl<K: slotmap::Key, V> Default for Arena<K, V> {
9    fn default() -> Self {
10        Self { inner: SlotMap::with_key() }
11    }
12}
13
14impl<K: slotmap::Key, V> Arena<K, V> {
15    pub fn new() -> Self {
16        Self::default()
17    }
18    pub fn insert(&mut self, value: V) -> K {
19        self.inner.insert(value)
20    }
21    pub fn get(&self, id: K) -> Option<&V> {
22        self.inner.get(id)
23    }
24    pub fn get_mut(&mut self, id: K) -> Option<&mut V> {
25        self.inner.get_mut(id)
26    }
27    pub fn remove(&mut self, id: K) -> Option<V> {
28        self.inner.remove(id)
29    }
30    pub fn contains(&self, id: K) -> bool {
31        self.inner.contains_key(id)
32    }
33    pub fn len(&self) -> usize {
34        self.inner.len()
35    }
36    pub fn is_empty(&self) -> bool {
37        self.inner.is_empty()
38    }
39    /// 迭代所有 (key, value) 对(测试 / 调试用)
40    pub fn iter(&self) -> impl Iterator<Item = (K, &V)> {
41        self.inner.iter()
42    }
43}
44
45// #[cfg(test)] 已迁到 crates/mgui-test/tests/arena.rs