Skip to main content

reifydb_runtime/sync/map/
mod.rs

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