reifydb_runtime/sync/map/
mod.rs1use std::{borrow::Borrow, hash::Hash};
7
8use cfg_if::cfg_if;
9
10#[cfg(reifydb_target = "native")]
11pub(crate) mod native;
12
13#[cfg(reifydb_target = "wasm")]
14pub(crate) mod wasm;
15
16cfg_if! {
17 if #[cfg(reifydb_target = "native")] {
18 type MapInnerImpl<K, V> = native::MapInner<K, V>;
19 } else {
20 type MapInnerImpl<K, V> = wasm::MapInner<K, V>;
21 }
22}
23
24pub struct Map<K, V>
26where
27 K: Eq + Hash,
28{
29 inner: MapInnerImpl<K, V>,
30}
31
32impl<K, V> Map<K, V>
33where
34 K: Eq + Hash,
35{
36 #[inline]
37 pub fn new() -> Self {
38 Self {
39 inner: MapInnerImpl::new(),
40 }
41 }
42
43 #[inline]
44 pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
45 where
46 F: FnOnce() -> V,
47 V: Clone,
48 K: Clone,
49 {
50 self.inner.get_or_insert_with(key, f)
51 }
52
53 #[inline]
54 pub fn get<Q>(&self, key: &Q) -> Option<V>
55 where
56 K: Borrow<Q>,
57 Q: Hash + Eq + ?Sized,
58 V: Clone,
59 {
60 self.inner.get(key)
61 }
62
63 #[inline]
64 pub fn contains_key<Q>(&self, key: &Q) -> bool
65 where
66 K: Borrow<Q>,
67 Q: Hash + Eq + ?Sized,
68 {
69 self.inner.contains_key(key)
70 }
71
72 #[inline]
73 pub fn with_read<Q, R, F>(&self, key: &Q, f: F) -> Option<R>
74 where
75 K: Borrow<Q>,
76 Q: Hash + Eq + ?Sized,
77 F: FnOnce(&V) -> R,
78 {
79 self.inner.with_read(key, f)
80 }
81
82 #[inline]
83 pub fn insert(&self, key: K, value: V)
84 where
85 K: Clone,
86 {
87 self.inner.insert(key, value);
88 }
89
90 #[inline]
91 pub fn remove<Q>(&self, key: &Q) -> Option<V>
92 where
93 K: Borrow<Q>,
94 Q: Hash + Eq + ?Sized,
95 {
96 self.inner.remove(key)
97 }
98
99 #[inline]
100 pub fn keys(&self) -> Vec<K>
101 where
102 K: Clone,
103 {
104 self.inner.keys()
105 }
106
107 #[inline]
108 pub fn with_write<Q, R, F>(&self, key: &Q, f: F) -> Option<R>
109 where
110 K: Borrow<Q>,
111 Q: Hash + Eq + ?Sized,
112 F: FnOnce(&mut V) -> R,
113 {
114 self.inner.with_write(key, f)
115 }
116
117 #[inline]
118 pub fn clear(&self) {
119 self.inner.clear();
120 }
121}
122
123impl<K, V> Default for Map<K, V>
124where
125 K: Eq + Hash,
126{
127 #[inline]
128 fn default() -> Self {
129 Self::new()
130 }
131}