1use indexmap::map::{
2 IndexMap as Map, IntoIter as MapIntoIter, Iter as MapIter, IterMut as MapIterMut,
3};
4use parking_lot::Mutex;
5use serde::{Deserializer, Serialize, Serializer};
6use std::borrow::Borrow;
7use std::cell::UnsafeCell;
8use std::fmt::{Debug, Display, Formatter};
9use std::hash::Hash;
10use std::marker::PhantomData;
11use std::ops::{Deref, DerefMut};
12use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
13use std::sync::Arc;
14
15use super::{ReadGuard, ReadMapGuard, WriteGuard, WriteLock};
16
17pub type IndexMapGet<'a, V> = ReadGuard<'a, V>;
19
20pub struct IndexMapRefMut<'a, K, V> {
22 inner: WriteGuard<'a, V>,
23 _k: PhantomData<&'a K>,
24}
25
26impl<'a, K, V> IndexMapRefMut<'a, K, V> {
27 #[inline]
28 pub(crate) fn new(inner: WriteGuard<'a, V>) -> Self {
29 IndexMapRefMut {
30 inner,
31 _k: PhantomData,
32 }
33 }
34}
35
36impl<'a, K, V> Deref for IndexMapRefMut<'a, K, V> {
37 type Target = V;
38
39 #[inline]
40 fn deref(&self) -> &Self::Target {
41 &self.inner
42 }
43}
44
45impl<'a, K, V> DerefMut for IndexMapRefMut<'a, K, V> {
46 #[inline]
47 fn deref_mut(&mut self) -> &mut Self::Target {
48 &mut self.inner
49 }
50}
51
52impl<'a, K, V: Debug> Debug for IndexMapRefMut<'a, K, V> {
53 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54 Debug::fmt(&*self.inner, f)
55 }
56}
57
58impl<'a, K, V: Display> Display for IndexMapRefMut<'a, K, V> {
59 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60 Display::fmt(&*self.inner, f)
61 }
62}
63
64impl<'a, K, V: PartialEq> PartialEq for IndexMapRefMut<'a, K, V> {
65 fn eq(&self, other: &Self) -> bool {
66 *self.inner == *other.inner
67 }
68}
69
70impl<'a, K, V: Eq> Eq for IndexMapRefMut<'a, K, V> {}
71
72pub type HashMapRefMut<'a, K, V> = IndexMapRefMut<'a, K, V>;
75
76pub struct IndexMapIter<'a, K, V> {
78 count: &'a AtomicUsize,
79 inner: MapIter<'a, K, V>,
80 _not_send: PhantomData<*const ()>,
81}
82
83impl<'a, K, V> Drop for IndexMapIter<'a, K, V> {
84 fn drop(&mut self) {
85 self.count.fetch_sub(1, Ordering::Release);
86 }
87}
88
89impl<'a, K, V> Iterator for IndexMapIter<'a, K, V> {
90 type Item = (&'a K, &'a V);
91
92 fn next(&mut self) -> Option<Self::Item> {
93 self.inner.next()
94 }
95}
96
97pub struct IndexMapIterMut<'a, K, V> {
99 _w: WriteLock<'a>,
100 inner: MapIterMut<'a, K, V>,
101}
102
103impl<'a, K, V> Iterator for IndexMapIterMut<'a, K, V> {
104 type Item = (&'a K, &'a mut V);
105
106 fn next(&mut self) -> Option<Self::Item> {
107 self.inner.next()
108 }
109}
110
111pub struct SyncIndexMap<K: Eq + Hash, V> {
126 dirty: UnsafeCell<Map<K, V>>,
127 write: Mutex<()>,
128 id: usize,
129 writing: AtomicBool,
130 registry: Mutex<Vec<std::boxed::Box<AtomicUsize>>>,
131}
132
133unsafe impl<K: Eq + Hash, V: Send> Send for SyncIndexMap<K, V> {}
137unsafe impl<K: Eq + Hash, V: Sync> Sync for SyncIndexMap<K, V> {}
138
139impl<K, V> SyncIndexMap<K, V>
140where
141 K: Eq + Hash,
142{
143 #[inline]
144 fn begin_read(&self) -> &AtomicUsize {
145 let count = super::reader_count_for(self.id, &self.registry);
149 loop {
150 count.fetch_add(1, Ordering::SeqCst);
151 if !self.writing.load(Ordering::SeqCst) {
152 return count;
153 }
154 count.fetch_sub(1, Ordering::SeqCst);
155 std::thread::yield_now();
156 }
157 }
158
159 #[inline]
160 fn begin_write(&self) -> WriteLock<'_> {
161 let lock = self.write.lock();
162 self.writing.store(true, Ordering::SeqCst);
163 loop {
164 let registry = self.registry.lock();
165 let all_zero = registry.iter().all(|c| c.load(Ordering::SeqCst) == 0);
166 if all_zero {
167 break;
168 }
169 drop(registry);
170 std::thread::yield_now();
171 }
172 WriteLock::new(lock, &self.writing)
173 }
174
175 pub fn new_arc() -> Arc<Self> {
176 Arc::new(Self::new())
177 }
178
179 pub fn new() -> Self {
180 Self {
181 dirty: UnsafeCell::new(Map::new()),
182 write: Mutex::new(()),
183 id: super::CONTAINER_ID.fetch_add(1, Ordering::Relaxed),
184 writing: AtomicBool::new(false),
185 registry: Mutex::new(Vec::new()),
186 }
187 }
188
189 pub fn with_capacity(capacity: usize) -> Self {
190 Self {
191 dirty: UnsafeCell::new(Map::with_capacity(capacity)),
192 write: Mutex::new(()),
193 id: super::CONTAINER_ID.fetch_add(1, Ordering::Relaxed),
194 writing: AtomicBool::new(false),
195 registry: Mutex::new(Vec::new()),
196 }
197 }
198
199 pub fn with_map(map: Map<K, V>) -> Self {
200 Self {
201 dirty: UnsafeCell::new(map),
202 write: Mutex::new(()),
203 id: super::CONTAINER_ID.fetch_add(1, Ordering::Relaxed),
204 writing: AtomicBool::new(false),
205 registry: Mutex::new(Vec::new()),
206 }
207 }
208
209 pub fn insert(&self, k: K, v: V) -> Option<V> {
210 let _w = self.begin_write();
211 unsafe { &mut *self.dirty.get() }.insert(k, v)
212 }
213
214 pub fn insert_mut(&mut self, k: K, v: V) -> Option<V> {
215 unsafe { &mut *self.dirty.get() }.insert(k, v)
216 }
217
218 pub fn remove(&self, k: &K) -> Option<V> {
219 let _w = self.begin_write();
220 unsafe { &mut *self.dirty.get() }.swap_remove(k)
221 }
222
223 pub fn remove_mut(&mut self, k: &K) -> Option<V> {
224 unsafe { &mut *self.dirty.get() }.swap_remove(k)
225 }
226
227 pub fn len(&self) -> usize {
228 let count = self.begin_read();
229 let n = unsafe { &*self.dirty.get() }.len();
230 count.fetch_sub(1, Ordering::Release);
231 n
232 }
233
234 pub fn is_empty(&self) -> bool {
235 let count = self.begin_read();
236 let b = unsafe { &*self.dirty.get() }.is_empty();
237 count.fetch_sub(1, Ordering::Release);
238 b
239 }
240
241 pub fn clear(&self) {
242 let _w = self.begin_write();
243 unsafe { &mut *self.dirty.get() }.clear();
244 }
245
246 pub fn clear_mut(&mut self) {
247 unsafe { &mut *self.dirty.get() }.clear();
248 }
249
250 pub fn shrink_to_fit(&self) {
251 let _w = self.begin_write();
252 unsafe { &mut *self.dirty.get() }.shrink_to_fit();
253 }
254
255 pub fn shrink_to_fit_mut(&mut self) {
256 unsafe { &mut *self.dirty.get() }.shrink_to_fit()
257 }
258
259 pub fn from(map: Map<K, V>) -> Self
260 where
261 K: Eq + Hash,
262 {
263 Self::with_map(map)
264 }
265
266 #[inline]
287 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<IndexMapGet<'_, V>>
288 where
289 K: Borrow<Q>,
290 Q: Hash + Eq,
291 {
292 let count = self.begin_read();
293 let m = unsafe { &*self.dirty.get() };
294 match m.get(k) {
295 Some(v) => Some(ReadGuard::new(count, v)),
296 None => {
297 count.fetch_sub(1, Ordering::Release);
298 None
299 }
300 }
301 }
302
303 #[inline]
310 pub fn get_mut(&self, k: &K) -> Option<IndexMapRefMut<'_, K, V>> {
311 let w = self.begin_write();
312 let m = unsafe { &mut *self.dirty.get() };
313 match m.get_mut(k) {
314 Some(v) => Some(IndexMapRefMut::new(WriteGuard::new(w, v))),
315 None => None,
316 }
317 }
318
319 #[inline]
320 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
321 where
322 K: Borrow<Q>,
323 Q: Hash + Eq,
324 {
325 let count = self.begin_read();
326 let b = unsafe { &*self.dirty.get() }.contains_key(k);
327 count.fetch_sub(1, Ordering::Release);
328 b
329 }
330
331 pub fn iter(&self) -> IndexMapIter<'_, K, V> {
332 let count = self.begin_read();
333 let m = unsafe { &*self.dirty.get() };
334 IndexMapIter {
335 count,
336 inner: m.iter(),
337 _not_send: PhantomData,
338 }
339 }
340
341 pub fn iter_mut(&self) -> IndexMapIterMut<'_, K, V> {
342 let w = self.begin_write();
343 let m = unsafe { &mut *self.dirty.get() };
344 IndexMapIterMut {
345 _w: w,
346 inner: m.iter_mut(),
347 }
348 }
349
350 pub fn into_iter(self) -> MapIntoIter<K, V> {
351 self.into_inner().into_iter()
352 }
353
354 pub fn dirty_ref(&self) -> ReadMapGuard<'_, Map<K, V>> {
355 let count = self.begin_read();
356 let m = unsafe { &*self.dirty.get() };
357 ReadMapGuard::new(count, m)
358 }
359
360 pub fn into_inner(self) -> Map<K, V> {
361 self.dirty.into_inner()
362 }
363}
364
365impl<K, V> IntoIterator for SyncIndexMap<K, V>
366where
367 K: Eq + Hash,
368{
369 type Item = (K, V);
370 type IntoIter = MapIntoIter<K, V>;
371
372 fn into_iter(self) -> Self::IntoIter {
373 self.into_iter()
374 }
375}
376
377impl<K: Eq + Hash, V> From<Map<K, V>> for SyncIndexMap<K, V> {
378 fn from(arg: Map<K, V>) -> Self {
379 Self::from(arg)
380 }
381}
382
383impl<K, V> serde::Serialize for SyncIndexMap<K, V>
384where
385 K: Eq + Hash + Serialize,
386 V: Serialize,
387{
388 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
389 where
390 S: Serializer,
391 {
392 self.dirty_ref().serialize(serializer)
393 }
394}
395
396impl<'de, K, V> serde::Deserialize<'de> for SyncIndexMap<K, V>
397where
398 K: Eq + Hash + serde::Deserialize<'de>,
399 V: serde::Deserialize<'de>,
400{
401 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
402 where
403 D: Deserializer<'de>,
404 {
405 let m = Map::deserialize(deserializer)?;
406 Ok(Self::from(m))
407 }
408}
409
410impl<K, V> Debug for SyncIndexMap<K, V>
411where
412 K: Eq + Hash + Debug,
413 V: Debug,
414{
415 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
416 Debug::fmt(&*self.dirty_ref(), f)
417 }
418}
419
420impl<K, V> Display for SyncIndexMap<K, V>
421where
422 K: Eq + Hash + Debug,
423 V: Debug,
424{
425 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
426 Debug::fmt(&*self.dirty_ref(), f)
427 }
428}
429
430impl<K: Clone + Eq + Hash, V: Clone> Clone for SyncIndexMap<K, V> {
431 fn clone(&self) -> Self {
432 let c = (*self.dirty_ref()).clone();
433 SyncIndexMap::from(c)
434 }
435}
436
437impl<K: Eq + Hash, V> Default for SyncIndexMap<K, V> {
438 fn default() -> Self {
439 SyncIndexMap::new()
440 }
441}