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
use super::one::RefMut;
use crate::util;
use crate::HashMap;
use parking_lot::RwLockWriteGuard;
use std::hash::Hash;
use std::mem;
use std::ptr;

pub enum Entry<'a, K: Eq + Hash, V> {
    Occupied(OccupiedEntry<'a, K, V>),
    Vacant(VacantEntry<'a, K, V>),
}

impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {
    /// Apply a function to the stored value if it exists.
    #[inline]
    pub fn and_modify(self, f: impl FnOnce(&mut V)) -> Self {
        match self {
            Entry::Occupied(mut entry) => {
                f(entry.get_mut());
                Entry::Occupied(entry)
            }

            Entry::Vacant(entry) => Entry::Vacant(entry),
        }
    }

    /// Get the key of the entry.
    #[inline]
    pub fn key(&self) -> &K {
        match *self {
            Entry::Occupied(ref entry) => entry.key(),
            Entry::Vacant(ref entry) => entry.key(),
        }
    }

    /// Return a mutable reference to the element if it exists,
    /// otherwise insert the default and return a mutable reference to that.
    #[inline]
    pub fn or_default(self) -> RefMut<'a, K, V>
    where
        V: Default,
    {
        match self {
            Entry::Occupied(entry) => entry.into_ref(),
            Entry::Vacant(entry) => entry.insert(V::default()),
        }
    }

    /// Return a mutable reference to the element if it exists,
    /// otherwise a provided value and return a mutable reference to that.
    #[inline]
    pub fn or_insert(self, value: V) -> RefMut<'a, K, V> {
        match self {
            Entry::Occupied(entry) => entry.into_ref(),
            Entry::Vacant(entry) => entry.insert(value),
        }
    }

    /// Return a mutable reference to the element if it exists,
    /// otherwise insert the result of a provided function and return a mutable reference to that.
    #[inline]
    pub fn or_insert_with(self, value: impl FnOnce() -> V) -> RefMut<'a, K, V> {
        match self {
            Entry::Occupied(entry) => entry.into_ref(),
            Entry::Vacant(entry) => entry.insert(value()),
        }
    }
}

pub struct VacantEntry<'a, K: Eq + Hash, V> {
    shard: RwLockWriteGuard<'a, HashMap<K, V>>,
    key: K,
}

unsafe impl<'a, K: Eq + Hash + Send, V: Send> Send for VacantEntry<'a, K, V> {}
unsafe impl<'a, K: Eq + Hash + Send + Sync, V: Send + Sync> Sync for VacantEntry<'a, K, V> {}

impl<'a, K: Eq + Hash, V> VacantEntry<'a, K, V> {
    #[inline]
    pub(crate) fn new(shard: RwLockWriteGuard<'a, HashMap<K, V>>, key: K) -> Self {
        Self { shard, key }
    }

    #[inline]
    pub fn insert(mut self, value: V) -> RefMut<'a, K, V> {
        unsafe {
            let c: K = ptr::read(&self.key);
            self.shard.insert(self.key, value);
            let (k, v) = self.shard.get_key_value(&c).unwrap();
            let k = util::change_lifetime_const(k);
            let v = util::change_lifetime_mut(util::to_mut(v));
            let r = RefMut::new(self.shard, k, v);
            mem::forget(c);
            r
        }
    }

    #[inline]
    pub fn into_key(self) -> K {
        self.key
    }

    #[inline]
    pub fn key(&self) -> &K {
        &self.key
    }
}

pub struct OccupiedEntry<'a, K: Eq + Hash, V> {
    shard: RwLockWriteGuard<'a, HashMap<K, V>>,
    elem: (&'a K, &'a mut V),
    key: Option<K>,
}

unsafe impl<'a, K: Eq + Hash + Send, V: Send> Send for OccupiedEntry<'a, K, V> {}
unsafe impl<'a, K: Eq + Hash + Send + Sync, V: Send + Sync> Sync for OccupiedEntry<'a, K, V> {}

impl<'a, K: Eq + Hash, V> OccupiedEntry<'a, K, V> {
    #[inline]
    pub(crate) fn new(
        shard: RwLockWriteGuard<'a, HashMap<K, V>>,
        key: Option<K>,
        elem: (&'a K, &'a mut V),
    ) -> Self {
        Self { shard, elem, key }
    }

    #[inline]
    pub fn get(&self) -> &V {
        self.elem.1
    }

    #[inline]
    pub fn get_mut(&mut self) -> &mut V {
        self.elem.1
    }

    #[inline]
    pub fn insert(&mut self, value: V) -> V {
        mem::replace(self.elem.1, value)
    }

    #[inline]
    pub fn into_ref(self) -> RefMut<'a, K, V> {
        RefMut::new(self.shard, self.elem.0, self.elem.1)
    }

    #[inline]
    pub fn key(&self) -> &K {
        self.elem.0
    }

    #[inline]
    pub fn remove(mut self) -> V {
        self.shard.remove(self.elem.0).unwrap()
    }

    #[inline]
    pub fn remove_entry(mut self) -> (K, V) {
        self.shard.remove_entry(self.elem.0).unwrap()
    }

    #[inline]
    pub fn replace_entry(mut self, value: V) -> (K, V) {
        let nk = self.key.unwrap();
        let p = self.shard.remove_entry(self.elem.0).unwrap();
        self.shard.insert(nk, value);
        p
    }
}