neocache 1.0.0

A concurrent HashMap with S3-FIFO cache eviction, forked from DashMap
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Single-key reference types returned by `get` and `get_mut`.
use crate::HashMap;
use crate::lock::{RwLockReadGuard, RwLockWriteGuard};
use core::hash::Hash;
use core::ops::{Deref, DerefMut};
use std::fmt::{Debug, Formatter};

/// A shared reference to a single map entry, holding a per-shard read lock.
pub struct Ref<'a, K, V> {
    _guard: RwLockReadGuard<'a, HashMap<K, V>>,
    k: *const K,
    v: *const V,
}

// SAFETY: `Ref` carries a read guard and raw pointers into the locked
// shard. The read guard from our vendored `RawRwLock` does not require
// unlock-on-the-locking-thread, so it is safe to send across threads.
// `K`/`V` need only be `Sync` because access through `Ref` is read-only.
unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Send for Ref<'a, K, V> {}
// SAFETY: see the `Send` impl above.
unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Sync for Ref<'a, K, V> {}

impl<'a, K: Eq + Hash, V> Ref<'a, K, V> {
    /// # Safety
    ///
    /// `k` and `v` must point to a key/value pair stored inside the
    /// `HashMap<K, V>` (the per-shard `ShardData`) protected by `guard`,
    /// and the entry must not be removed or relocated for the lifetime
    /// `'a`. The lock guard is what keeps the storage alive and pinned.
    pub(crate) unsafe fn new(
        guard: RwLockReadGuard<'a, HashMap<K, V>>,
        k: *const K,
        v: *const V,
    ) -> Self {
        Self {
            _guard: guard,
            k,
            v,
        }
    }

    /// Returns a reference to the key.
    pub fn key(&self) -> &K {
        self.pair().0
    }

    /// Returns a reference to the value.
    pub fn value(&self) -> &V {
        self.pair().1
    }

    /// Returns a `(&key, &value)` tuple.
    pub fn pair(&self) -> (&K, &V) {
        // SAFETY: `Ref::new`'s contract requires `self.k`/`self.v` to be
        // valid for the lifetime of the held read guard `_guard`. The
        // shared borrow of `&self` keeps that lifetime active and the
        // read lock excludes any concurrent writer.
        unsafe { (&*self.k, &*self.v) }
    }

    /// Projects the reference onto a sub-field of the value, keeping the lock held.
    pub fn map<F, T>(self, f: F) -> MappedRef<'a, K, V, T>
    where
        F: FnOnce(&V) -> &T,
    {
        MappedRef {
            _guard: self._guard,
            k: self.k,
            // SAFETY: `self.v` upholds the `Ref::new` contract; the read
            // guard is moved into the resulting `MappedRef`, so the lock
            // remains held for the projected reference's lifetime.
            v: f(unsafe { &*self.v }),
        }
    }

    /// Like [`map`](Self::map) but returns `Err(self)` if the projection returns `None`.
    pub fn try_map<F, T>(self, f: F) -> Result<MappedRef<'a, K, V, T>, Self>
    where
        F: FnOnce(&V) -> Option<&T>,
    {
        // SAFETY: see the `map` impl above.
        if let Some(v) = f(unsafe { &*self.v }) {
            Ok(MappedRef {
                _guard: self._guard,
                k: self.k,
                v,
            })
        } else {
            Err(self)
        }
    }
}

impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for Ref<'a, K, V> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Ref")
            .field("k", self.key())
            .field("v", self.value())
            .finish()
    }
}

impl<'a, K: Eq + Hash, V> Deref for Ref<'a, K, V> {
    type Target = V;

    fn deref(&self) -> &V {
        self.value()
    }
}

/// A mutable reference to a single map entry, holding a per-shard write lock.
pub struct RefMut<'a, K, V> {
    guard: RwLockWriteGuard<'a, HashMap<K, V>>,
    k: *const K,
    v: *mut V,
}

// SAFETY: see the `Send`/`Sync` argument on `Ref` above. `RefMut` differs
// only in the guard kind — a write guard excludes both readers and writers,
// so the bound is the same.
unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Send for RefMut<'a, K, V> {}
// SAFETY: see the `Send` impl above.
unsafe impl<'a, K: Eq + Hash + Sync, V: Sync> Sync for RefMut<'a, K, V> {}

impl<'a, K: Eq + Hash, V> RefMut<'a, K, V> {
    /// # Safety
    ///
    /// `k` and `v` must point to a key/value pair stored inside the
    /// `HashMap<K, V>` (the per-shard `ShardData`) protected by `guard`,
    /// and the entry must not be removed or relocated for the lifetime
    /// `'a`. The write guard is what keeps the storage alive, pinned, and
    /// exclusively borrowed.
    pub(crate) unsafe fn new(
        guard: RwLockWriteGuard<'a, HashMap<K, V>>,
        k: *const K,
        v: *mut V,
    ) -> Self {
        Self { guard, k, v }
    }

    /// Returns a reference to the key.
    pub fn key(&self) -> &K {
        self.pair().0
    }

    /// Returns a shared reference to the value.
    pub fn value(&self) -> &V {
        self.pair().1
    }

    /// Returns a mutable reference to the value.
    pub fn value_mut(&mut self) -> &mut V {
        self.pair_mut().1
    }

    /// Returns a `(&key, &value)` tuple.
    pub fn pair(&self) -> (&K, &V) {
        // SAFETY: see `RefMut::new`. `&self` excludes any concurrent
        // `pair_mut`/`value_mut` call on this `RefMut`.
        unsafe { (&*self.k, &*self.v) }
    }

    /// Returns a `(&key, &mut value)` tuple.
    pub fn pair_mut(&mut self) -> (&K, &mut V) {
        // SAFETY: `&mut self` is unique by the borrow checker; the held
        // write guard excludes every other thread; together these give
        // exclusive access to the entry.
        unsafe { (&*self.k, &mut *self.v) }
    }

    /// Atomically downgrades this write guard to a shared read guard.
    pub fn downgrade(self) -> Ref<'a, K, V> {
        // SAFETY: `RwLockWriteGuard::downgrade` atomically converts the
        // exclusive lock to a shared one without releasing it. The
        // pointers `self.k`/`self.v` continue to satisfy `Ref::new`'s
        // contract under the (now shared) lock.
        unsafe { Ref::new(RwLockWriteGuard::downgrade(self.guard), self.k, self.v) }
    }

    /// Projects the mutable reference onto a sub-field of the value.
    pub fn map<F, T>(self, f: F) -> MappedRefMut<'a, K, V, T>
    where
        F: FnOnce(&mut V) -> &mut T,
    {
        MappedRefMut {
            _guard: self.guard,
            k: self.k,
            // SAFETY: `self` is consumed, so the `&mut V` we hand to `f`
            // is the only live reference to the value. The write guard
            // moves into the returned `MappedRefMut`, keeping the lock
            // held for the projected reference's lifetime.
            v: f(unsafe { &mut *self.v }),
        }
    }

    /// Like [`map`](Self::map) but returns `Err(self)` if the projection returns `None`.
    pub fn try_map<F, T>(self, f: F) -> Result<MappedRefMut<'a, K, V, T>, Self>
    where
        F: FnOnce(&mut V) -> Option<&mut T>,
    {
        // SAFETY: same as the `map` impl above. The cast `*mut V as
        // *mut _` only changes the inferred pointee type; the underlying
        // address remains the value pointer and is reborrowed exclusively.
        let v = match f(unsafe { &mut *(self.v as *mut _) }) {
            Some(v) => v,
            None => return Err(self),
        };
        let guard = self.guard;
        let k = self.k;
        Ok(MappedRefMut {
            _guard: guard,
            k,
            v,
        })
    }
}

impl<'a, K: Eq + Hash + Debug, V: Debug> Debug for RefMut<'a, K, V> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RefMut")
            .field("k", self.key())
            .field("v", self.value())
            .finish()
    }
}

impl<'a, K: Eq + Hash, V> Deref for RefMut<'a, K, V> {
    type Target = V;

    fn deref(&self) -> &V {
        self.value()
    }
}

impl<'a, K: Eq + Hash, V> DerefMut for RefMut<'a, K, V> {
    fn deref_mut(&mut self) -> &mut V {
        self.value_mut()
    }
}

/// A reference to a projected sub-field of a map value, holding a read lock.
pub struct MappedRef<'a, K, V, T> {
    _guard: RwLockReadGuard<'a, HashMap<K, V>>,
    k: *const K,
    v: *const T,
}

impl<'a, K: Eq + Hash, V, T> MappedRef<'a, K, V, T> {
    /// Returns a reference to the key.
    pub fn key(&self) -> &K {
        self.pair().0
    }

    /// Returns a reference to the projected value.
    pub fn value(&self) -> &T {
        self.pair().1
    }

    /// Returns a `(&key, &projected_value)` tuple.
    pub fn pair(&self) -> (&K, &T) {
        // SAFETY: `MappedRef` was constructed from a `Ref` whose pointers
        // satisfied `Ref::new`'s contract; the projection function returned
        // a sub-reference of the value, which is also valid for the same
        // read-lock lifetime `'a`.
        unsafe { (&*self.k, &*self.v) }
    }

    /// Further projects onto a sub-field.
    pub fn map<F, T2>(self, f: F) -> MappedRef<'a, K, V, T2>
    where
        F: FnOnce(&T) -> &T2,
    {
        MappedRef {
            _guard: self._guard,
            k: self.k,
            // SAFETY: see `pair` above. The read guard is moved into the
            // new `MappedRef` so the projected reference outlives the
            // call.
            v: f(unsafe { &*self.v }),
        }
    }

    /// Like [`map`](Self::map) but returns `Err(self)` if the projection returns `None`.
    pub fn try_map<F, T2>(self, f: F) -> Result<MappedRef<'a, K, V, T2>, Self>
    where
        F: FnOnce(&T) -> Option<&T2>,
    {
        // SAFETY: see the `map` impl above.
        let v = match f(unsafe { &*self.v }) {
            Some(v) => v,
            None => return Err(self),
        };
        let guard = self._guard;
        Ok(MappedRef {
            _guard: guard,
            k: self.k,
            v,
        })
    }
}

impl<'a, K: Eq + Hash + Debug, V, T: Debug> Debug for MappedRef<'a, K, V, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MappedRef")
            .field("k", self.key())
            .field("v", self.value())
            .finish()
    }
}

impl<'a, K: Eq + Hash, V, T> Deref for MappedRef<'a, K, V, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.value()
    }
}

impl<'a, K: Eq + Hash, V, T: std::fmt::Display> std::fmt::Display for MappedRef<'a, K, V, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self.value(), f)
    }
}

impl<'a, K: Eq + Hash, V, T: AsRef<TDeref>, TDeref: ?Sized> AsRef<TDeref>
    for MappedRef<'a, K, V, T>
{
    fn as_ref(&self) -> &TDeref {
        self.value().as_ref()
    }
}

/// A mutable reference to a projected sub-field of a map value, holding a write lock.
pub struct MappedRefMut<'a, K, V, T> {
    _guard: RwLockWriteGuard<'a, HashMap<K, V>>,
    k: *const K,
    v: *mut T,
}

impl<'a, K: Eq + Hash, V, T> MappedRefMut<'a, K, V, T> {
    /// Returns a reference to the key.
    pub fn key(&self) -> &K {
        self.pair().0
    }

    /// Returns a shared reference to the projected value.
    pub fn value(&self) -> &T {
        self.pair().1
    }

    /// Returns a mutable reference to the projected value.
    pub fn value_mut(&mut self) -> &mut T {
        self.pair_mut().1
    }

    /// Returns a `(&key, &projected_value)` tuple.
    pub fn pair(&self) -> (&K, &T) {
        // SAFETY: see the corresponding `MappedRef::pair`. The held write
        // guard excludes other threads; `&self` excludes other accesses
        // through this `MappedRefMut`.
        unsafe { (&*self.k, &*self.v) }
    }

    /// Returns a `(&key, &mut projected_value)` tuple.
    pub fn pair_mut(&mut self) -> (&K, &mut T) {
        // SAFETY: `&mut self` is unique by the borrow checker; the held
        // write guard excludes every other thread.
        unsafe { (&*self.k, &mut *self.v) }
    }

    /// Further projects onto a sub-field mutably.
    pub fn map<F, T2>(self, f: F) -> MappedRefMut<'a, K, V, T2>
    where
        F: FnOnce(&mut T) -> &mut T2,
    {
        MappedRefMut {
            _guard: self._guard,
            k: self.k,
            // SAFETY: `self` is consumed; the projection produces the only
            // live `&mut T` to the entry. The write guard moves into the
            // returned `MappedRefMut`.
            v: f(unsafe { &mut *self.v }),
        }
    }

    /// Like [`map`](Self::map) but returns `Err(self)` if the projection returns `None`.
    pub fn try_map<F, T2>(self, f: F) -> Result<MappedRefMut<'a, K, V, T2>, Self>
    where
        F: FnOnce(&mut T) -> Option<&mut T2>,
    {
        // SAFETY: see the `map` impl above; the cast only changes the
        // inferred pointee type.
        let v = match f(unsafe { &mut *(self.v as *mut _) }) {
            Some(v) => v,
            None => return Err(self),
        };
        let guard = self._guard;
        let k = self.k;
        Ok(MappedRefMut {
            _guard: guard,
            k,
            v,
        })
    }
}

impl<'a, K: Eq + Hash + Debug, V, T: Debug> Debug for MappedRefMut<'a, K, V, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MappedRefMut")
            .field("k", self.key())
            .field("v", self.value())
            .finish()
    }
}

impl<'a, K: Eq + Hash, V, T> Deref for MappedRefMut<'a, K, V, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.value()
    }
}

impl<'a, K: Eq + Hash, V, T> DerefMut for MappedRefMut<'a, K, V, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.value_mut()
    }
}