/rust/registry/src/index.crates.io-1949cf8c6b5b557f/dashmap-6.1.0/src/mapref/one.rs
Line | Count | Source |
1 | | use crate::lock::{RwLockReadGuard, RwLockWriteGuard}; |
2 | | use crate::HashMap; |
3 | | use core::hash::Hash; |
4 | | use core::ops::{Deref, DerefMut}; |
5 | | use std::fmt::{Debug, Formatter}; |
6 | | |
7 | | pub struct Ref<'a, K, V> { |
8 | | _guard: RwLockReadGuard<'a, HashMap<K, V>>, |
9 | | k: *const K, |
10 | | v: *const V, |
11 | | } |
12 | | |
13 | | unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Send for Ref<'a, K, V> {} |
14 | | unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Sync for Ref<'a, K, V> {} |
15 | | |
16 | | impl<'a, K: Eq + Hash, V> Ref<'a, K, V> { |
17 | 0 | pub(crate) unsafe fn new( |
18 | 0 | guard: RwLockReadGuard<'a, HashMap<K, V>>, |
19 | 0 | k: *const K, |
20 | 0 | v: *const V, |
21 | 0 | ) -> Self { |
22 | 0 | Self { |
23 | 0 | _guard: guard, |
24 | 0 | k, |
25 | 0 | v, |
26 | 0 | } |
27 | 0 | } Unexecuted instantiation: <dashmap::mapref::one::Ref<alloc::string::String, surrealdb_core::idx::ft::analyzer::mapper::Mapper>>::new Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::connection::BucketConnectionKey, alloc::sync::Arc<dyn surrealdb_core::buc::store::ObjectStore>>>::new Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::store::util::path::ObjectKey, surrealdb_core::buc::store::memory::Entry>>::new Unexecuted instantiation: <dashmap::mapref::one::Ref<u64, surrealdb_core::idx::trees::vector::SharedVector>>::new Unexecuted instantiation: <dashmap::mapref::one::Ref<_, _>>::new |
28 | | |
29 | 0 | pub fn key(&self) -> &K { |
30 | 0 | self.pair().0 |
31 | 0 | } |
32 | | |
33 | 0 | pub fn value(&self) -> &V { |
34 | 0 | self.pair().1 |
35 | 0 | } Unexecuted instantiation: <dashmap::mapref::one::Ref<alloc::string::String, surrealdb_core::idx::ft::analyzer::mapper::Mapper>>::value Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::connection::BucketConnectionKey, alloc::sync::Arc<dyn surrealdb_core::buc::store::ObjectStore>>>::value Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::store::util::path::ObjectKey, surrealdb_core::buc::store::memory::Entry>>::value Unexecuted instantiation: <dashmap::mapref::one::Ref<u64, surrealdb_core::idx::trees::vector::SharedVector>>::value Unexecuted instantiation: <dashmap::mapref::one::Ref<_, _>>::value |
36 | | |
37 | 0 | pub fn pair(&self) -> (&K, &V) { |
38 | 0 | unsafe { (&*self.k, &*self.v) } |
39 | 0 | } Unexecuted instantiation: <dashmap::mapref::one::Ref<alloc::string::String, surrealdb_core::idx::ft::analyzer::mapper::Mapper>>::pair Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::connection::BucketConnectionKey, alloc::sync::Arc<dyn surrealdb_core::buc::store::ObjectStore>>>::pair Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::store::util::path::ObjectKey, surrealdb_core::buc::store::memory::Entry>>::pair Unexecuted instantiation: <dashmap::mapref::one::Ref<u64, surrealdb_core::idx::trees::vector::SharedVector>>::pair Unexecuted instantiation: <dashmap::mapref::one::Ref<_, _>>::pair |
40 | | |
41 | 0 | pub fn map<F, T>(self, f: F) -> MappedRef<'a, K, V, T> |
42 | 0 | where |
43 | 0 | F: FnOnce(&V) -> &T, |
44 | | { |
45 | 0 | MappedRef { |
46 | 0 | _guard: self._guard, |
47 | 0 | k: self.k, |
48 | 0 | v: f(unsafe { &*self.v }), |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | 0 | pub fn try_map<F, T>(self, f: F) -> Result<MappedRef<'a, K, V, T>, Self> |
53 | 0 | where |
54 | 0 | F: FnOnce(&V) -> Option<&T>, |
55 | | { |
56 | 0 | if let Some(v) = f(unsafe { &*self.v }) { |
57 | 0 | Ok(MappedRef { |
58 | 0 | _guard: self._guard, |
59 | 0 | k: self.k, |
60 | 0 | v, |
61 | 0 | }) |
62 | | } else { |
63 | 0 | Err(self) |
64 | | } |
65 | 0 | } |
66 | | } |
67 | | |
68 | | impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for Ref<'a, K, V> { |
69 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
70 | 0 | f.debug_struct("Ref") |
71 | 0 | .field("k", &self.k) |
72 | 0 | .field("v", &self.v) |
73 | 0 | .finish() |
74 | 0 | } |
75 | | } |
76 | | |
77 | | impl<'a, K: Eq + Hash, V> Deref for Ref<'a, K, V> { |
78 | | type Target = V; |
79 | | |
80 | 0 | fn deref(&self) -> &V { |
81 | 0 | self.value() |
82 | 0 | } Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::connection::BucketConnectionKey, alloc::sync::Arc<dyn surrealdb_core::buc::store::ObjectStore>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <dashmap::mapref::one::Ref<surrealdb_core::buc::store::util::path::ObjectKey, surrealdb_core::buc::store::memory::Entry> as core::ops::deref::Deref>::deref Unexecuted instantiation: <dashmap::mapref::one::Ref<_, _> as core::ops::deref::Deref>::deref |
83 | | } |
84 | | |
85 | | pub struct RefMut<'a, K, V> { |
86 | | guard: RwLockWriteGuard<'a, HashMap<K, V>>, |
87 | | k: *const K, |
88 | | v: *mut V, |
89 | | } |
90 | | |
91 | | unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Send for RefMut<'a, K, V> {} |
92 | | unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Sync for RefMut<'a, K, V> {} |
93 | | |
94 | | impl<'a, K: Eq + Hash, V> RefMut<'a, K, V> { |
95 | 0 | pub(crate) unsafe fn new( |
96 | 0 | guard: RwLockWriteGuard<'a, HashMap<K, V>>, |
97 | 0 | k: *const K, |
98 | 0 | v: *mut V, |
99 | 0 | ) -> Self { |
100 | 0 | Self { guard, k, v } |
101 | 0 | } Unexecuted instantiation: <dashmap::mapref::one::RefMut<alloc::vec::Vec<u8>, alloc::sync::Arc<surrealdb_core::idx::trees::store::cache::TreeCache<surrealdb_core::idx::trees::mtree::MTreeNode>>>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<alloc::string::String, surrealdb_core::idx::ft::analyzer::mapper::Mapper>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<surrealdb_core::buc::connection::BucketConnectionKey, alloc::sync::Arc<dyn surrealdb_core::buc::store::ObjectStore>>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<surrealdb_core::buc::store::util::path::ObjectKey, surrealdb_core::buc::store::memory::Entry>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<u64, alloc::sync::Arc<surrealdb_core::idx::trees::store::StoredNode<surrealdb_core::idx::trees::mtree::MTreeNode>>>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<u64, surrealdb_core::idx::trees::vector::SharedVector>>::new Unexecuted instantiation: <dashmap::mapref::one::RefMut<_, _>>::new |
102 | | |
103 | 0 | pub fn key(&self) -> &K { |
104 | 0 | self.pair().0 |
105 | 0 | } |
106 | | |
107 | 0 | pub fn value(&self) -> &V { |
108 | 0 | self.pair().1 |
109 | 0 | } |
110 | | |
111 | 0 | pub fn value_mut(&mut self) -> &mut V { |
112 | 0 | self.pair_mut().1 |
113 | 0 | } |
114 | | |
115 | 0 | pub fn pair(&self) -> (&K, &V) { |
116 | 0 | unsafe { (&*self.k, &*self.v) } |
117 | 0 | } |
118 | | |
119 | 0 | pub fn pair_mut(&mut self) -> (&K, &mut V) { |
120 | 0 | unsafe { (&*self.k, &mut *self.v) } |
121 | 0 | } |
122 | | |
123 | 0 | pub fn downgrade(self) -> Ref<'a, K, V> { |
124 | 0 | unsafe { Ref::new(RwLockWriteGuard::downgrade(self.guard), self.k, self.v) } |
125 | 0 | } |
126 | | |
127 | 0 | pub fn map<F, T>(self, f: F) -> MappedRefMut<'a, K, V, T> |
128 | 0 | where |
129 | 0 | F: FnOnce(&mut V) -> &mut T, |
130 | | { |
131 | 0 | MappedRefMut { |
132 | 0 | _guard: self.guard, |
133 | 0 | k: self.k, |
134 | 0 | v: f(unsafe { &mut *self.v }), |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | 0 | pub fn try_map<F, T>(self, f: F) -> Result<MappedRefMut<'a, K, V, T>, Self> |
139 | 0 | where |
140 | 0 | F: FnOnce(&mut V) -> Option<&mut T>, |
141 | | { |
142 | 0 | let v = match f(unsafe { &mut *(self.v as *mut _) }) { |
143 | 0 | Some(v) => v, |
144 | 0 | None => return Err(self), |
145 | | }; |
146 | 0 | let guard = self.guard; |
147 | 0 | let k = self.k; |
148 | 0 | Ok(MappedRefMut { |
149 | 0 | _guard: guard, |
150 | 0 | k, |
151 | 0 | v, |
152 | 0 | }) |
153 | 0 | } |
154 | | } |
155 | | |
156 | | impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for RefMut<'a, K, V> { |
157 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
158 | 0 | f.debug_struct("RefMut") |
159 | 0 | .field("k", &self.k) |
160 | 0 | .field("v", &self.v) |
161 | 0 | .finish() |
162 | 0 | } |
163 | | } |
164 | | |
165 | | impl<'a, K: Eq + Hash, V> Deref for RefMut<'a, K, V> { |
166 | | type Target = V; |
167 | | |
168 | 0 | fn deref(&self) -> &V { |
169 | 0 | self.value() |
170 | 0 | } |
171 | | } |
172 | | |
173 | | impl<'a, K: Eq + Hash, V> DerefMut for RefMut<'a, K, V> { |
174 | 0 | fn deref_mut(&mut self) -> &mut V { |
175 | 0 | self.value_mut() |
176 | 0 | } |
177 | | } |
178 | | |
179 | | pub struct MappedRef<'a, K, V, T> { |
180 | | _guard: RwLockReadGuard<'a, HashMap<K, V>>, |
181 | | k: *const K, |
182 | | v: *const T, |
183 | | } |
184 | | |
185 | | impl<'a, K: Eq + Hash, V, T> MappedRef<'a, K, V, T> { |
186 | 0 | pub fn key(&self) -> &K { |
187 | 0 | self.pair().0 |
188 | 0 | } |
189 | | |
190 | 0 | pub fn value(&self) -> &T { |
191 | 0 | self.pair().1 |
192 | 0 | } |
193 | | |
194 | 0 | pub fn pair(&self) -> (&K, &T) { |
195 | 0 | unsafe { (&*self.k, &*self.v) } |
196 | 0 | } |
197 | | |
198 | 0 | pub fn map<F, T2>(self, f: F) -> MappedRef<'a, K, V, T2> |
199 | 0 | where |
200 | 0 | F: FnOnce(&T) -> &T2, |
201 | | { |
202 | 0 | MappedRef { |
203 | 0 | _guard: self._guard, |
204 | 0 | k: self.k, |
205 | 0 | v: f(unsafe { &*self.v }), |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | pub fn try_map<F, T2>(self, f: F) -> Result<MappedRef<'a, K, V, T2>, Self> |
210 | 0 | where |
211 | 0 | F: FnOnce(&T) -> Option<&T2>, |
212 | | { |
213 | 0 | let v = match f(unsafe { &*self.v }) { |
214 | 0 | Some(v) => v, |
215 | 0 | None => return Err(self), |
216 | | }; |
217 | 0 | let guard = self._guard; |
218 | 0 | Ok(MappedRef { |
219 | 0 | _guard: guard, |
220 | 0 | k: self.k, |
221 | 0 | v, |
222 | 0 | }) |
223 | 0 | } |
224 | | } |
225 | | |
226 | | impl<'a, K: Eq + Hash + Debug, V, T: Debug> Debug for MappedRef<'a, K, V, T> { |
227 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
228 | 0 | f.debug_struct("MappedRef") |
229 | 0 | .field("k", &self.k) |
230 | 0 | .field("v", &self.v) |
231 | 0 | .finish() |
232 | 0 | } |
233 | | } |
234 | | |
235 | | impl<'a, K: Eq + Hash, V, T> Deref for MappedRef<'a, K, V, T> { |
236 | | type Target = T; |
237 | | |
238 | 0 | fn deref(&self) -> &T { |
239 | 0 | self.value() |
240 | 0 | } |
241 | | } |
242 | | |
243 | | impl<'a, K: Eq + Hash, V, T: std::fmt::Display> std::fmt::Display for MappedRef<'a, K, V, T> { |
244 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
245 | 0 | std::fmt::Display::fmt(self.value(), f) |
246 | 0 | } |
247 | | } |
248 | | |
249 | | impl<'a, K: Eq + Hash, V, T: AsRef<TDeref>, TDeref: ?Sized> AsRef<TDeref> |
250 | | for MappedRef<'a, K, V, T> |
251 | | { |
252 | 0 | fn as_ref(&self) -> &TDeref { |
253 | 0 | self.value().as_ref() |
254 | 0 | } |
255 | | } |
256 | | |
257 | | pub struct MappedRefMut<'a, K, V, T> { |
258 | | _guard: RwLockWriteGuard<'a, HashMap<K, V>>, |
259 | | k: *const K, |
260 | | v: *mut T, |
261 | | } |
262 | | |
263 | | impl<'a, K: Eq + Hash, V, T> MappedRefMut<'a, K, V, T> { |
264 | 0 | pub fn key(&self) -> &K { |
265 | 0 | self.pair().0 |
266 | 0 | } |
267 | | |
268 | 0 | pub fn value(&self) -> &T { |
269 | 0 | self.pair().1 |
270 | 0 | } |
271 | | |
272 | 0 | pub fn value_mut(&mut self) -> &mut T { |
273 | 0 | self.pair_mut().1 |
274 | 0 | } |
275 | | |
276 | 0 | pub fn pair(&self) -> (&K, &T) { |
277 | 0 | unsafe { (&*self.k, &*self.v) } |
278 | 0 | } |
279 | | |
280 | 0 | pub fn pair_mut(&mut self) -> (&K, &mut T) { |
281 | 0 | unsafe { (&*self.k, &mut *self.v) } |
282 | 0 | } |
283 | | |
284 | 0 | pub fn map<F, T2>(self, f: F) -> MappedRefMut<'a, K, V, T2> |
285 | 0 | where |
286 | 0 | F: FnOnce(&mut T) -> &mut T2, |
287 | | { |
288 | 0 | MappedRefMut { |
289 | 0 | _guard: self._guard, |
290 | 0 | k: self.k, |
291 | 0 | v: f(unsafe { &mut *self.v }), |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 0 | pub fn try_map<F, T2>(self, f: F) -> Result<MappedRefMut<'a, K, V, T2>, Self> |
296 | 0 | where |
297 | 0 | F: FnOnce(&mut T) -> Option<&mut T2>, |
298 | | { |
299 | 0 | let v = match f(unsafe { &mut *(self.v as *mut _) }) { |
300 | 0 | Some(v) => v, |
301 | 0 | None => return Err(self), |
302 | | }; |
303 | 0 | let guard = self._guard; |
304 | 0 | let k = self.k; |
305 | 0 | Ok(MappedRefMut { |
306 | 0 | _guard: guard, |
307 | 0 | k, |
308 | 0 | v, |
309 | 0 | }) |
310 | 0 | } |
311 | | } |
312 | | |
313 | | impl<'a, K: Eq + Hash + Debug, V, T: Debug> Debug for MappedRefMut<'a, K, V, T> { |
314 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
315 | 0 | f.debug_struct("MappedRefMut") |
316 | 0 | .field("k", &self.k) |
317 | 0 | .field("v", &self.v) |
318 | 0 | .finish() |
319 | 0 | } |
320 | | } |
321 | | |
322 | | impl<'a, K: Eq + Hash, V, T> Deref for MappedRefMut<'a, K, V, T> { |
323 | | type Target = T; |
324 | | |
325 | 0 | fn deref(&self) -> &T { |
326 | 0 | self.value() |
327 | 0 | } |
328 | | } |
329 | | |
330 | | impl<'a, K: Eq + Hash, V, T> DerefMut for MappedRefMut<'a, K, V, T> { |
331 | 0 | fn deref_mut(&mut self) -> &mut T { |
332 | 0 | self.value_mut() |
333 | 0 | } |
334 | | } |