|
|
@ -37,22 +37,27 @@ use parking_lot::RwLockWriteGuard;
|
|
|
|
use std::collections::hash_map::RandomState;
|
|
|
|
use std::collections::hash_map::RandomState;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A read-only reference to a bucket
|
|
|
|
pub struct Ref<'a, K, V> {
|
|
|
|
pub struct Ref<'a, K, V> {
|
|
|
|
_guard: RwLockReadGuard<'a, LowMap<K, V>>,
|
|
|
|
_g: RwLockReadGuard<'a, LowMap<K, V>>,
|
|
|
|
k: &'a K,
|
|
|
|
k: &'a K,
|
|
|
|
v: &'a V,
|
|
|
|
v: &'a V,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V> Ref<'a, K, V> {
|
|
|
|
impl<'a, K, V> Ref<'a, K, V> {
|
|
|
|
pub const fn new(_guard: RwLockReadGuard<'a, LowMap<K, V>>, k: &'a K, v: &'a V) -> Self {
|
|
|
|
/// Create a new reference
|
|
|
|
Self { _guard, k, v }
|
|
|
|
pub(super) const fn new(_g: RwLockReadGuard<'a, LowMap<K, V>>, k: &'a K, v: &'a V) -> Self {
|
|
|
|
|
|
|
|
Self { _g, k, v }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
self.k
|
|
|
|
self.k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the value
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key/value pair
|
|
|
|
pub const fn pair(&self) -> (&K, &V) {
|
|
|
|
pub const fn pair(&self) -> (&K, &V) {
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
(k, v)
|
|
|
|
(k, v)
|
|
|
@ -69,32 +74,35 @@ impl<'a, K, V> Deref for Ref<'a, K, V> {
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for Ref<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for Ref<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for Ref<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for Ref<'a, K, V> {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A r/w ref to a bucket
|
|
|
|
pub struct RefMut<'a, K, V> {
|
|
|
|
pub struct RefMut<'a, K, V> {
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
_g: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
k: &'a K,
|
|
|
|
k: &'a K,
|
|
|
|
v: &'a mut V,
|
|
|
|
v: &'a mut V,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V> RefMut<'a, K, V> {
|
|
|
|
impl<'a, K, V> RefMut<'a, K, V> {
|
|
|
|
pub fn new(guard: RwLockWriteGuard<'a, LowMap<K, V>>, k: &'a K, v: &'a mut V) -> Self {
|
|
|
|
/// Create a new ref
|
|
|
|
Self { guard, k, v }
|
|
|
|
pub(super) fn new(_g: RwLockWriteGuard<'a, LowMap<K, V>>, k: &'a K, v: &'a mut V) -> Self {
|
|
|
|
|
|
|
|
Self { _g, k, v }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
self.k
|
|
|
|
self.k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the value
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable ref to the value
|
|
|
|
pub fn value_mut(&mut self) -> &mut V {
|
|
|
|
pub fn value_mut(&mut self) -> &mut V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the k/v pair
|
|
|
|
pub fn pair(&mut self) -> (&K, &V) {
|
|
|
|
pub fn pair(&mut self) -> (&K, &V) {
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
(k, v)
|
|
|
|
(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn downgrade_ref(self) -> Ref<'a, K, V> {
|
|
|
|
|
|
|
|
Ref::new(RwLockWriteGuard::downgrade(self.guard), self.k, self.v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V> Deref for RefMut<'a, K, V> {
|
|
|
|
impl<'a, K, V> Deref for RefMut<'a, K, V> {
|
|
|
@ -113,6 +121,7 @@ impl<'a, K, V> DerefMut for RefMut<'a, K, V> {
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for RefMut<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for RefMut<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for RefMut<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for RefMut<'a, K, V> {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A reference to an occupied entry
|
|
|
|
pub struct OccupiedEntry<'a, K, V, S> {
|
|
|
|
pub struct OccupiedEntry<'a, K, V, S> {
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
elem: (&'a K, &'a mut V),
|
|
|
|
elem: (&'a K, &'a mut V),
|
|
|
@ -121,7 +130,8 @@ pub struct OccupiedEntry<'a, K, V, S> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> {
|
|
|
|
impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> {
|
|
|
|
pub fn new(
|
|
|
|
/// Create a new occupied entry ref
|
|
|
|
|
|
|
|
pub(super) fn new(
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
key: K,
|
|
|
|
key: K,
|
|
|
|
elem: (&'a K, &'a mut V),
|
|
|
|
elem: (&'a K, &'a mut V),
|
|
|
@ -134,15 +144,19 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> {
|
|
|
|
hasher,
|
|
|
|
hasher,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
self.elem.0
|
|
|
|
self.elem.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the value
|
|
|
|
pub fn value(&self) -> &V {
|
|
|
|
pub fn value(&self) -> &V {
|
|
|
|
self.elem.1
|
|
|
|
self.elem.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a value into this bucket
|
|
|
|
pub fn insert(&mut self, other: V) -> V {
|
|
|
|
pub fn insert(&mut self, other: V) -> V {
|
|
|
|
mem::replace(self.elem.1, other)
|
|
|
|
mem::replace(self.elem.1, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove this element from the map
|
|
|
|
pub fn remove(mut self) -> V {
|
|
|
|
pub fn remove(mut self) -> V {
|
|
|
|
let hash = super::make_hash::<K, K, S>(&self.hasher, &self.key);
|
|
|
|
let hash = super::make_hash::<K, K, S>(&self.hasher, &self.key);
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
@ -157,6 +171,7 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> {
|
|
|
|
unsafe impl<'a, K: Send, V: Send, S> Send for OccupiedEntry<'a, K, V, S> {}
|
|
|
|
unsafe impl<'a, K: Send, V: Send, S> Send for OccupiedEntry<'a, K, V, S> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync, S> Sync for OccupiedEntry<'a, K, V, S> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync, S> Sync for OccupiedEntry<'a, K, V, S> {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A ref to a vacant entry
|
|
|
|
pub struct VacantEntry<'a, K, V, S> {
|
|
|
|
pub struct VacantEntry<'a, K, V, S> {
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
guard: RwLockWriteGuard<'a, LowMap<K, V>>,
|
|
|
|
key: K,
|
|
|
|
key: K,
|
|
|
@ -164,9 +179,11 @@ pub struct VacantEntry<'a, K, V, S> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K: Hash + Eq, V, S: BuildHasher> VacantEntry<'a, K, V, S> {
|
|
|
|
impl<'a, K: Hash + Eq, V, S: BuildHasher> VacantEntry<'a, K, V, S> {
|
|
|
|
pub fn new(guard: RwLockWriteGuard<'a, LowMap<K, V>>, key: K, hasher: S) -> Self {
|
|
|
|
/// Create a vacant entry ref
|
|
|
|
|
|
|
|
pub(super) fn new(guard: RwLockWriteGuard<'a, LowMap<K, V>>, key: K, hasher: S) -> Self {
|
|
|
|
Self { guard, key, hasher }
|
|
|
|
Self { guard, key, hasher }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a value into this bucket
|
|
|
|
pub fn insert(mut self, value: V) -> RefMut<'a, K, V> {
|
|
|
|
pub fn insert(mut self, value: V) -> RefMut<'a, K, V> {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
let hash = super::make_insert_hash::<K, S>(&self.hasher, &self.key);
|
|
|
|
let hash = super::make_insert_hash::<K, S>(&self.hasher, &self.key);
|
|
|
@ -180,28 +197,34 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> VacantEntry<'a, K, V, S> {
|
|
|
|
RefMut::new(self.guard, kptr, vptr)
|
|
|
|
RefMut::new(self.guard, kptr, vptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns self into a key (effectively freeing up the entry for another thread)
|
|
|
|
pub fn into_key(self) -> K {
|
|
|
|
pub fn into_key(self) -> K {
|
|
|
|
self.key
|
|
|
|
self.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
&self.key
|
|
|
|
&self.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// An entry, either occupied or vacant
|
|
|
|
pub enum Entry<'a, K, V, S = RandomState> {
|
|
|
|
pub enum Entry<'a, K, V, S = RandomState> {
|
|
|
|
Occupied(OccupiedEntry<'a, K, V, S>),
|
|
|
|
Occupied(OccupiedEntry<'a, K, V, S>),
|
|
|
|
Vacant(VacantEntry<'a, K, V, S>),
|
|
|
|
Vacant(VacantEntry<'a, K, V, S>),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V, S> Entry<'a, K, V, S> {
|
|
|
|
impl<'a, K, V, S> Entry<'a, K, V, S> {
|
|
|
|
|
|
|
|
/// Check if an entry is occupied
|
|
|
|
pub const fn is_occupied(&self) -> bool {
|
|
|
|
pub const fn is_occupied(&self) -> bool {
|
|
|
|
matches!(self, Self::Occupied(_))
|
|
|
|
matches!(self, Self::Occupied(_))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if an entry is vacant
|
|
|
|
pub const fn is_vacant(&self) -> bool {
|
|
|
|
pub const fn is_vacant(&self) -> bool {
|
|
|
|
matches!(self, Self::Vacant(_))
|
|
|
|
matches!(self, Self::Vacant(_))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A shared ref to a key
|
|
|
|
pub struct RefMulti<'a, K, V> {
|
|
|
|
pub struct RefMulti<'a, K, V> {
|
|
|
|
_g: Arc<RwLockReadGuard<'a, LowMap<K, V>>>,
|
|
|
|
_g: Arc<RwLockReadGuard<'a, LowMap<K, V>>>,
|
|
|
|
k: &'a K,
|
|
|
|
k: &'a K,
|
|
|
@ -209,15 +232,19 @@ pub struct RefMulti<'a, K, V> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V> RefMulti<'a, K, V> {
|
|
|
|
impl<'a, K, V> RefMulti<'a, K, V> {
|
|
|
|
|
|
|
|
/// Create a new shared ref
|
|
|
|
pub const fn new(_g: Arc<RwLockReadGuard<'a, LowMap<K, V>>>, k: &'a K, v: &'a V) -> Self {
|
|
|
|
pub const fn new(_g: Arc<RwLockReadGuard<'a, LowMap<K, V>>>, k: &'a K, v: &'a V) -> Self {
|
|
|
|
Self { _g, k, v }
|
|
|
|
Self { _g, k, v }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
self.k
|
|
|
|
self.k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the value
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the k/v pair
|
|
|
|
pub const fn pair(&self) -> (&K, &V) {
|
|
|
|
pub const fn pair(&self) -> (&K, &V) {
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
(k, v)
|
|
|
|
(k, v)
|
|
|
@ -234,6 +261,7 @@ impl<'a, K, V> Deref for RefMulti<'a, K, V> {
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for RefMulti<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Sync, V: Sync> Sync for RefMulti<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for RefMulti<'a, K, V> {}
|
|
|
|
unsafe impl<'a, K: Send, V: Send> Send for RefMulti<'a, K, V> {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A shared r/w ref to a bucket
|
|
|
|
pub struct RefMultiMut<'a, K, V> {
|
|
|
|
pub struct RefMultiMut<'a, K, V> {
|
|
|
|
_g: Arc<RwLockWriteGuard<'a, LowMap<K, V>>>,
|
|
|
|
_g: Arc<RwLockWriteGuard<'a, LowMap<K, V>>>,
|
|
|
|
k: &'a K,
|
|
|
|
k: &'a K,
|
|
|
@ -241,22 +269,28 @@ pub struct RefMultiMut<'a, K, V> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, K, V> RefMultiMut<'a, K, V> {
|
|
|
|
impl<'a, K, V> RefMultiMut<'a, K, V> {
|
|
|
|
|
|
|
|
/// Create a new shared r/w ref
|
|
|
|
pub fn new(_g: Arc<RwLockWriteGuard<'a, LowMap<K, V>>>, k: &'a K, v: &'a mut V) -> Self {
|
|
|
|
pub fn new(_g: Arc<RwLockWriteGuard<'a, LowMap<K, V>>>, k: &'a K, v: &'a mut V) -> Self {
|
|
|
|
Self { _g, k, v }
|
|
|
|
Self { _g, k, v }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the key
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
pub const fn key(&self) -> &K {
|
|
|
|
self.k
|
|
|
|
self.k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the value
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
pub const fn value(&self) -> &V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable ref to the value
|
|
|
|
pub fn value_mut(&mut self) -> &mut V {
|
|
|
|
pub fn value_mut(&mut self) -> &mut V {
|
|
|
|
self.v
|
|
|
|
self.v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a ref to the k/v pair
|
|
|
|
pub fn pair(&self) -> (&K, &V) {
|
|
|
|
pub fn pair(&self) -> (&K, &V) {
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
(k, v)
|
|
|
|
(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable ref to the k/v (k, mut v) pair
|
|
|
|
pub fn pair_mut(&mut self) -> (&K, &mut V) {
|
|
|
|
pub fn pair_mut(&mut self) -> (&K, &mut V) {
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
let Self { k, v, .. } = self;
|
|
|
|
(k, v)
|
|
|
|
(k, v)
|
|
|
|