use crate::hash_table::LockedEntry;
use super::ebr::{Arc, AtomicArc, Barrier, Tag};
use super::hash_table::bucket::{EntryPtr, Evictable, Locker, Reader, CACHE};
use super::hash_table::bucket_array::BucketArray;
use super::hash_table::HashTable;
use super::wait_queue::AsyncWait;
use std::borrow::Borrow;
use std::collections::hash_map::RandomState;
use std::fmt::{self, Debug};
use std::hash::{BuildHasher, Hash};
use std::mem::replace;
use std::ops::RangeInclusive;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::{Acquire, Relaxed};
pub struct HashCache<K, V, H = RandomState>
where
K: Eq + Hash,
H: BuildHasher,
{
array: AtomicArc<BucketArray<K, Evictable<V>, CACHE>>,
minimum_capacity: AtomicUsize,
maximum_capacity: usize,
build_hasher: H,
}
pub const DEFAULT_MAXIMUM_CAPACITY: usize = 256;
pub type EvictedEntry<K, V> = Option<(K, V)>;
pub enum Entry<'h, K, V, H = RandomState>
where
K: Eq + Hash,
H: BuildHasher,
{
Occupied(OccupiedEntry<'h, K, V, H>),
Vacant(VacantEntry<'h, K, V, H>),
}
pub struct OccupiedEntry<'h, K, V, H = RandomState>
where
K: Eq + Hash,
H: BuildHasher,
{
hashcache: &'h HashCache<K, V, H>,
locked_entry: LockedEntry<'h, K, Evictable<V>, CACHE>,
}
pub struct VacantEntry<'h, K, V, H = RandomState>
where
K: Eq + Hash,
H: BuildHasher,
{
hashcache: &'h HashCache<K, V, H>,
key: K,
hash: u64,
locked_entry: LockedEntry<'h, K, Evictable<V>, CACHE>,
}
impl<K, V, H> HashCache<K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn with_hasher(build_hasher: H) -> Self {
HashCache {
array: AtomicArc::null(),
minimum_capacity: AtomicUsize::new(0),
maximum_capacity: DEFAULT_MAXIMUM_CAPACITY,
build_hasher,
}
}
#[inline]
pub fn with_capacity_and_hasher(
minimum_capacity: usize,
maximum_capacity: usize,
build_hasher: H,
) -> Self {
let (array, minimum_capacity) = if minimum_capacity == 0 {
(AtomicArc::null(), AtomicUsize::new(0))
} else {
let array = unsafe {
Arc::new_unchecked(BucketArray::<K, Evictable<V>, CACHE>::new(
minimum_capacity,
AtomicArc::null(),
))
};
let minimum_capacity = array.num_entries();
(AtomicArc::from(array), AtomicUsize::new(minimum_capacity))
};
let maximum_capacity = maximum_capacity
.max(minimum_capacity.load(Relaxed))
.max(BucketArray::<K, Evictable<V>, CACHE>::minimum_capacity())
.min(1_usize << (usize::BITS - 1))
.next_power_of_two();
HashCache {
array,
minimum_capacity,
maximum_capacity,
build_hasher,
}
}
#[inline]
pub fn entry(&self, key: K) -> Entry<K, V, H> {
let barrier = Barrier::new();
let hash = self.hash(&key);
let mut locked_entry = unsafe {
self.reserve_entry(&key, hash, &mut (), self.prolonged_barrier_ref(&barrier))
.ok()
.unwrap_unchecked()
};
if locked_entry.entry_ptr.is_valid() {
locked_entry
.locker
.update_lru_tail(locked_entry.data_block_mut, &locked_entry.entry_ptr);
Entry::Occupied(OccupiedEntry {
hashcache: self,
locked_entry,
})
} else {
Entry::Vacant(VacantEntry {
hashcache: self,
key,
hash,
locked_entry,
})
}
}
#[inline]
pub async fn entry_async(&self, key: K) -> Entry<K, V, H> {
let hash = self.hash(&key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let barrier = Barrier::new();
if let Ok(mut locked_entry) = self.reserve_entry(
&key,
hash,
&mut async_wait_pinned,
self.prolonged_barrier_ref(&barrier),
) {
if locked_entry.entry_ptr.is_valid() {
locked_entry
.locker
.update_lru_tail(locked_entry.data_block_mut, &locked_entry.entry_ptr);
return Entry::Occupied(OccupiedEntry {
hashcache: self,
locked_entry,
});
}
return Entry::Vacant(VacantEntry {
hashcache: self,
key,
hash,
locked_entry,
});
}
}
async_wait_pinned.await;
}
}
#[inline]
pub fn put(&self, key: K, val: V) -> Result<EvictedEntry<K, V>, (K, V)> {
let barrier = Barrier::new();
let hash = self.hash(&key);
let result = match self.reserve_entry(&key, hash, &mut (), &barrier) {
Ok(LockedEntry {
mut locker,
data_block_mut,
entry_ptr,
index: _,
}) => {
if entry_ptr.is_valid() {
return Err((key, val));
}
let evicted = locker
.evict_lru_head(data_block_mut)
.map(|(k, v)| (k, v.take()));
let entry_ptr = locker.insert_with(
data_block_mut,
BucketArray::<K, V, CACHE>::partial_hash(hash),
|| (key, Evictable::new(val)),
&barrier,
);
locker.update_lru_tail(data_block_mut, &entry_ptr);
Ok(evicted)
}
Err(_) => Err((key, val)),
};
result
}
#[inline]
pub async fn put_async(&self, key: K, val: V) -> Result<EvictedEntry<K, V>, (K, V)> {
let hash = self.hash(&key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let barrier = Barrier::new();
if let Ok(LockedEntry {
mut locker,
data_block_mut,
entry_ptr,
index: _,
}) = self.reserve_entry(&key, hash, &mut async_wait_pinned, &barrier)
{
if entry_ptr.is_valid() {
return Err((key, val));
}
let evicted = locker
.evict_lru_head(data_block_mut)
.map(|(k, v)| (k, v.take()));
let entry_ptr = locker.insert_with(
data_block_mut,
BucketArray::<K, V, CACHE>::partial_hash(hash),
|| (key, Evictable::new(val)),
&barrier,
);
locker.update_lru_tail(data_block_mut, &entry_ptr);
return Ok(evicted);
};
}
async_wait_pinned.await;
}
}
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<OccupiedEntry<K, V, H>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
let barrier = Barrier::new();
let mut locked_entry = self
.get_entry(
key,
self.hash(key.borrow()),
&mut (),
self.prolonged_barrier_ref(&barrier),
)
.ok()
.flatten()?;
locked_entry
.locker
.update_lru_tail(locked_entry.data_block_mut, &locked_entry.entry_ptr);
Some(OccupiedEntry {
hashcache: self,
locked_entry,
})
}
#[inline]
pub async fn get_async<Q>(&self, key: &Q) -> Option<OccupiedEntry<K, V, H>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if let Ok(result) = self.get_entry(
key,
hash,
&mut async_wait_pinned,
self.prolonged_barrier_ref(&Barrier::new()),
) {
if let Some(mut locked_entry) = result {
locked_entry
.locker
.update_lru_tail(locked_entry.data_block_mut, &locked_entry.entry_ptr);
return Some(OccupiedEntry {
hashcache: self,
locked_entry,
});
}
return None;
}
async_wait_pinned.await;
}
}
#[inline]
pub fn read<Q, R, F: FnOnce(&K, &V) -> R>(&self, key: &Q, reader: F) -> Option<R>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.read_entry(key, self.hash(key), &mut (), &Barrier::new())
.ok()
.flatten()
.map(|(k, v)| reader(k, v))
}
#[inline]
pub async fn read_async<Q, R, F: FnOnce(&K, &V) -> R>(&self, key: &Q, reader: F) -> Option<R>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if let Ok(result) = self.read_entry(key, hash, &mut async_wait_pinned, &Barrier::new())
{
return result.map(|(k, v)| reader(k, v));
}
async_wait_pinned.await;
}
}
#[inline]
pub fn contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.read(key, |_, _| ()).is_some()
}
#[inline]
pub async fn contains_async<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.read_async(key, |_, _| ()).await.is_some()
}
#[inline]
pub fn remove<Q>(&self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.remove_if(key, |_| true)
}
#[inline]
pub async fn remove_async<Q>(&self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.remove_if_async(key, |_| true).await
}
#[inline]
pub fn remove_if<Q, F: FnOnce(&mut V) -> bool>(&self, key: &Q, condition: F) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
self.get(key).and_then(|mut o| {
if condition(o.get_mut()) {
Some(o.remove_entry())
} else {
None
}
})
}
#[inline]
pub async fn remove_if_async<Q, F: FnOnce(&mut V) -> bool>(
&self,
key: &Q,
condition: F,
) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
if let Some(mut occupied_entry) = self.get_async(key).await {
if condition(occupied_entry.get_mut()) {
return Some(occupied_entry.remove_entry());
}
}
None
}
#[inline]
pub fn scan<F: FnMut(&K, &V)>(&self, mut scanner: F) {
let barrier = Barrier::new();
let mut current_array_ptr = self.array.load(Acquire, &barrier);
while let Some(current_array) = current_array_ptr.as_ref() {
self.clear_old_array(current_array, &barrier);
for index in 0..current_array.num_buckets() {
let bucket = current_array.bucket(index);
if let Some(locker) = Reader::lock(bucket, &barrier) {
let data_block = current_array.data_block(index);
let mut entry_ptr = EntryPtr::new(&barrier);
while entry_ptr.next(*locker, &barrier) {
let (k, v) = entry_ptr.get(data_block);
scanner(k, v);
}
}
}
let new_current_array_ptr = self.array.load(Acquire, &barrier);
if current_array_ptr.without_tag() == new_current_array_ptr.without_tag() {
break;
}
current_array_ptr = new_current_array_ptr;
}
}
#[inline]
pub async fn scan_async<F: FnMut(&K, &V)>(&self, mut scanner: F) {
let mut current_array_holder = self.array.get_arc(Acquire, &Barrier::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let barrier = Barrier::new();
let bucket = current_array.bucket(index);
if let Ok(reader) =
Reader::try_lock_or_wait(bucket, &mut async_wait_pinned, &barrier)
{
if let Some(reader) = reader {
let data_block = current_array.data_block(index);
let mut entry_ptr = EntryPtr::new(&barrier);
while entry_ptr.next(*reader, &barrier) {
let (k, v) = entry_ptr.get(data_block);
scanner(k, v);
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_arc(Acquire, &Barrier::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
current_array_holder.replace(new_current_array);
continue;
}
break;
}
}
#[inline]
pub fn any<P: FnMut(&K, &V) -> bool>(&self, mut pred: P) -> bool {
self.any_entry(|k, v| pred(k, v))
}
#[inline]
pub async fn any_async<P: FnMut(&K, &V) -> bool>(&self, mut pred: P) -> bool {
let mut current_array_holder = self.array.get_arc(Acquire, &Barrier::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let barrier = Barrier::new();
let bucket = current_array.bucket(index);
if let Ok(reader) =
Reader::try_lock_or_wait(bucket, &mut async_wait_pinned, &barrier)
{
if let Some(reader) = reader {
let data_block = current_array.data_block(index);
let mut entry_ptr = EntryPtr::new(&barrier);
while entry_ptr.next(*reader, &barrier) {
let (k, v) = entry_ptr.get(data_block);
if pred(k, v) {
return true;
}
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_arc(Acquire, &Barrier::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
current_array_holder.replace(new_current_array);
continue;
}
break;
}
false
}
#[inline]
pub fn for_each<F: FnMut(&K, &mut V)>(&self, mut f: F) {
self.retain(|k, v| {
f(k, v);
true
});
}
#[inline]
pub async fn for_each_async<F: FnMut(&K, &mut V)>(&self, mut f: F) {
self.retain_async(|k, v| {
f(k, v);
true
})
.await;
}
#[inline]
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&self, mut pred: F) -> (usize, usize) {
self.retain_entries(|k, v| pred(k, v))
}
#[inline]
pub async fn retain_async<F: FnMut(&K, &mut V) -> bool>(
&self,
mut filter: F,
) -> (usize, usize) {
let mut num_retained: usize = 0;
let mut num_removed: usize = 0;
let mut current_array_holder = self.array.get_arc(Acquire, &Barrier::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let barrier = Barrier::new();
let bucket = current_array.bucket_mut(index);
if let Ok(locker) =
Locker::try_lock_or_wait(bucket, &mut async_wait_pinned, &barrier)
{
if let Some(mut locker) = locker {
let data_block_mut = current_array.data_block_mut(index);
let mut entry_ptr = EntryPtr::new(&barrier);
while entry_ptr.next(&locker, &barrier) {
let (k, v) = entry_ptr.get_mut(data_block_mut, &mut locker);
if filter(k, v) {
num_retained = num_retained.saturating_add(1);
} else {
locker.erase(data_block_mut, &mut entry_ptr);
num_removed = num_removed.saturating_add(1);
}
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_arc(Acquire, &Barrier::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
num_retained = 0;
current_array_holder.replace(new_current_array);
continue;
}
break;
}
if num_removed >= num_retained {
self.try_resize(0, &Barrier::new());
}
(num_retained, num_removed)
}
#[inline]
pub fn clear(&self) -> usize {
self.retain(|_, _| false).1
}
#[inline]
pub async fn clear_async(&self) -> usize {
self.retain_async(|_, _| false).await.1
}
#[inline]
pub fn len(&self) -> usize {
self.num_entries(&Barrier::new())
}
#[inline]
pub fn is_empty(&self) -> bool {
!self.has_entry(&Barrier::new())
}
#[inline]
pub fn capacity(&self) -> usize {
self.num_slots(&Barrier::new())
}
#[inline]
pub fn capacity_range(&self) -> RangeInclusive<usize> {
self.minimum_capacity.load(Relaxed)..=self.maximum_capacity()
}
async fn cleanse_old_array_async(&self, current_array: &BucketArray<K, Evictable<V>, CACHE>) {
while !current_array.old_array(&Barrier::new()).is_null() {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if self.incremental_rehash::<_, _, false>(
current_array,
&mut async_wait_pinned,
&Barrier::new(),
) == Ok(true)
{
break;
}
async_wait_pinned.await;
}
}
}
impl<K, V> HashCache<K, V, RandomState>
where
K: Eq + Hash,
{
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub fn with_capacity(minimum_capacity: usize, maximum_capacity: usize) -> Self {
Self::with_capacity_and_hasher(minimum_capacity, maximum_capacity, RandomState::new())
}
}
impl<K, V, H> Default for HashCache<K, V, H>
where
K: Eq + Hash,
H: BuildHasher + Default,
{
#[inline]
fn default() -> Self {
Self::with_hasher(H::default())
}
}
impl<K, V, H> Debug for HashCache<K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_map();
self.scan(|k, v| {
d.entry(k, v);
});
d.finish()
}
}
impl<K, V, H> Drop for HashCache<K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn drop(&mut self) {
self.array
.swap((None, Tag::None), Relaxed)
.0
.map(|a| unsafe {
a.release_drop_in_place()
});
}
}
impl<K, V, H> HashTable<K, Evictable<V>, H, CACHE> for HashCache<K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn hasher(&self) -> &H {
&self.build_hasher
}
#[inline]
fn try_clone(_entry: &(K, Evictable<V>)) -> Option<(K, Evictable<V>)> {
None
}
#[inline]
fn try_reset(value: &mut Evictable<V>) {
value.reset_link();
}
#[inline]
fn bucket_array(&self) -> &AtomicArc<BucketArray<K, Evictable<V>, CACHE>> {
&self.array
}
#[inline]
fn minimum_capacity(&self) -> &AtomicUsize {
&self.minimum_capacity
}
#[inline]
fn maximum_capacity(&self) -> usize {
self.maximum_capacity
}
}
impl<K, V, H> PartialEq for HashCache<K, V, H>
where
K: Eq + Hash,
V: PartialEq,
H: BuildHasher,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
if !self.any(|k, v| other.read(k, |_, ov| v == ov) != Some(true)) {
return !other.any(|k, v| self.read(k, |_, sv| v == sv) != Some(true));
}
false
}
}
impl<'h, K, V, H> Entry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn or_put(self, val: V) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
self.or_put_with(|| val)
}
#[inline]
pub fn or_put_with<F: FnOnce() -> V>(
self,
constructor: F,
) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
self.or_put_with_key(|_| constructor())
}
#[inline]
pub fn or_put_with_key<F: FnOnce(&K) -> V>(
self,
constructor: F,
) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
match self {
Self::Occupied(o) => (None, o),
Self::Vacant(v) => {
let val = constructor(v.key());
v.put_entry(val)
}
}
}
#[inline]
pub fn key(&self) -> &K {
match self {
Self::Occupied(o) => o.key(),
Self::Vacant(v) => v.key(),
}
}
#[inline]
#[must_use]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Self::Occupied(mut o) => {
f(o.get_mut());
Self::Occupied(o)
}
Self::Vacant(_) => self,
}
}
#[inline]
pub fn put_entry(self, val: V) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
match self {
Self::Occupied(mut o) => {
o.put(val);
(None, o)
}
Self::Vacant(v) => v.put_entry(val),
}
}
}
impl<'h, K, V, H> Entry<'h, K, V, H>
where
K: Eq + Hash,
V: Default,
H: BuildHasher,
{
#[inline]
pub fn or_default(self) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
match self {
Self::Occupied(o) => (None, o),
Self::Vacant(v) => v.put_entry(Default::default()),
}
}
}
impl<'h, K, V, H> Debug for Entry<'h, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Vacant(v) => f.debug_tuple("Entry").field(v).finish(),
Self::Occupied(o) => f.debug_tuple("Entry").field(o).finish(),
}
}
}
impl<'h, K, V, H> OccupiedEntry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
#[must_use]
pub fn key(&self) -> &K {
&self
.locked_entry
.entry_ptr
.get(self.locked_entry.data_block_mut)
.0
}
#[inline]
#[must_use]
pub fn remove_entry(mut self) -> (K, V) {
let (k, v) = unsafe {
self.locked_entry.locker.remove_from_lru_list(
self.locked_entry.data_block_mut,
&self.locked_entry.entry_ptr,
);
self.locked_entry
.locker
.erase(
self.locked_entry.data_block_mut,
&mut self.locked_entry.entry_ptr,
)
.unwrap_unchecked()
};
if self.locked_entry.locker.num_entries() <= 1 || self.locked_entry.locker.need_rebuild() {
let barrier = Barrier::new();
let hashcache = self.hashcache;
if let Some(current_array) = hashcache.bucket_array().load(Acquire, &barrier).as_ref() {
if current_array.old_array(&barrier).is_null() {
let index = self.locked_entry.index;
if current_array.within_sampling_range(index) {
drop(self);
hashcache.try_shrink_or_rebuild(current_array, index, &barrier);
}
}
}
}
(k, v.take())
}
#[inline]
#[must_use]
pub fn get(&self) -> &V {
&self
.locked_entry
.entry_ptr
.get(self.locked_entry.data_block_mut)
.1
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
&mut self
.locked_entry
.entry_ptr
.get_mut(
self.locked_entry.data_block_mut,
&mut self.locked_entry.locker,
)
.1
}
#[inline]
pub fn put(&mut self, val: V) -> V {
replace(self.get_mut(), val)
}
#[inline]
#[must_use]
pub fn remove(self) -> V {
self.remove_entry().1
}
}
impl<'h, K, V, H> Debug for OccupiedEntry<'h, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("key", self.key())
.field("value", self.get())
.finish_non_exhaustive()
}
}
impl<'h, K, V, H> VacantEntry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn into_key(self) -> K {
self.key
}
#[inline]
pub fn put_entry(mut self, val: V) -> (EvictedEntry<K, V>, OccupiedEntry<'h, K, V, H>) {
let evicted = self
.locked_entry
.locker
.evict_lru_head(self.locked_entry.data_block_mut)
.map(|(k, v)| (k, v.take()));
let entry_ptr = self.locked_entry.locker.insert_with(
self.locked_entry.data_block_mut,
BucketArray::<K, V, CACHE>::partial_hash(self.hash),
|| (self.key, Evictable::new(val)),
self.hashcache.prolonged_barrier_ref(&Barrier::new()),
);
self.locked_entry
.locker
.update_lru_tail(self.locked_entry.data_block_mut, &entry_ptr);
let occupied = OccupiedEntry {
hashcache: self.hashcache,
locked_entry: LockedEntry {
index: self.locked_entry.index,
data_block_mut: self.locked_entry.data_block_mut,
locker: self.locked_entry.locker,
entry_ptr,
},
};
(evicted, occupied)
}
}
impl<'h, K, V, H> Debug for VacantEntry<'h, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("VacantEntry").field(self.key()).finish()
}
}