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
use crate::common::concurrent::ValueEntry;

use std::{
    hash::{BuildHasher, Hash},
    sync::Arc,
};
use triomphe::Arc as TrioArc;

type DashMapRef<'a, K, V, S> =
    dashmap::mapref::multiple::RefMulti<'a, Arc<K>, TrioArc<ValueEntry<K, V>>, S>;

pub struct EntryRef<'a, K, V, S>(DashMapRef<'a, K, V, S>);

unsafe impl<'a, K, V, S> Sync for EntryRef<'a, K, V, S>
where
    K: Eq + Hash + Send + Sync,
    V: Send + Sync,
    S: BuildHasher,
{
}

impl<'a, K, V, S> EntryRef<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    pub(crate) fn new(map_ref: DashMapRef<'a, K, V, S>) -> Self {
        Self(map_ref)
    }

    pub fn key(&self) -> &K {
        self.0.key()
    }

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

    pub fn pair(&self) -> (&K, &V) {
        (self.key(), self.value())
    }
}

impl<'a, K, V, S> std::ops::Deref for EntryRef<'a, K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    type Target = V;

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