wowlab_engine_domain/pool/
base.rs1use std::{hash::Hash, mem};
2
3use wowlab_types::sim::FastMap;
4
5#[derive(Clone, Debug)]
6pub(super) struct Pool<K, V> {
7 values: FastMap<K, V>,
8}
9
10impl<K, V> Pool<K, V>
11where
12 K: Eq + Hash,
13{
14 pub(super) fn get(&self, key: &K) -> Option<&V> {
15 self.values.get(key)
16 }
17
18 pub(super) fn get_mut(&mut self, key: &K) -> Option<&mut V> {
19 self.values.get_mut(key)
20 }
21
22 pub(super) fn set(&mut self, key: K, value: V) -> Option<V> {
23 self.values.insert(key, value)
24 }
25
26 pub(super) fn remove(&mut self, key: &K) -> Option<V> {
27 self.values.remove(key)
28 }
29
30 pub(super) fn clear(&mut self) {
31 self.values.clear();
32 }
33
34 pub(super) fn value_mut(&mut self, key: K) -> &mut V
35 where
36 V: Default,
37 {
38 self.values.entry(key).or_default()
39 }
40
41 pub(super) fn get_or_insert_with(&mut self, key: K, value: impl FnOnce() -> V) -> &mut V {
42 self.values.entry(key).or_insert_with(value)
43 }
44
45 pub(super) fn take(&mut self, key: &K) -> V
46 where
47 V: Default,
48 {
49 self.values.get_mut(key).map_or_else(V::default, mem::take)
50 }
51
52 pub(super) fn drain(&mut self) -> impl Iterator<Item = (K, V)> + '_ {
53 self.values.drain()
54 }
55}
56
57impl<K, V> Default for Pool<K, V> {
58 fn default() -> Self {
59 Self {
60 values: FastMap::default(),
61 }
62 }
63}
64
65#[cfg(test)]
66#[path = "base/tests.rs"]
67mod tests;