Skip to content

Commit 7c77eaf

Browse files
committed
chore: migrate from hashbrown::raw::RawTable to hashbrown::HashTable
as per rust-lang/hashbrown#546
1 parent cd04f71 commit 7c77eaf

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/stores/sized.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::Cached;
22
use crate::lru_list::LRUList;
3-
use hashbrown::raw::RawTable;
3+
use hashbrown::HashTable;
44
use std::cmp::Eq;
55
use std::fmt;
66
use std::hash::{BuildHasher, Hash, Hasher};
@@ -23,7 +23,7 @@ use {super::CachedAsync, async_trait::async_trait, futures::Future};
2323
#[derive(Clone)]
2424
pub struct SizedCache<K, V> {
2525
// `store` contains a hash of K -> index of (K, V) tuple in `order`
26-
pub(super) store: RawTable<usize>,
26+
pub(super) store: HashTable<usize>,
2727
pub(super) hash_builder: RandomState,
2828
pub(super) order: LRUList<(K, V)>,
2929
pub(super) capacity: usize,
@@ -88,7 +88,7 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
8888
panic!("`size` of `SizedCache` must be greater than zero.");
8989
}
9090
SizedCache {
91-
store: RawTable::with_capacity(size),
91+
store: HashTable::with_capacity(size),
9292
hash_builder: RandomState::new(),
9393
order: LRUList::<(K, V)>::with_capacity(size),
9494
capacity: size,
@@ -108,18 +108,20 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
108108
return Err(std::io::Error::from_raw_os_error(22));
109109
}
110110

111-
let store = match RawTable::try_with_capacity(size) {
112-
Ok(store) => store,
113-
Err(e) => {
114-
let errcode = match e {
115-
// ENOMEM
116-
hashbrown::TryReserveError::AllocError { .. } => 12,
117-
// EINVAL
118-
hashbrown::TryReserveError::CapacityOverflow => 22,
119-
};
120-
return Err(std::io::Error::from_raw_os_error(errcode));
121-
}
122-
};
111+
let mut store = HashTable::new();
112+
if let Err(e) = store.try_reserve(size, |&index: &usize| {
113+
let hasher = &mut RandomState::new().build_hasher();
114+
index.hash(hasher);
115+
hasher.finish()
116+
}) {
117+
let errcode = match e {
118+
// ENOMEM
119+
hashbrown::TryReserveError::AllocError { .. } => 12,
120+
// EINVAL
121+
hashbrown::TryReserveError::CapacityOverflow => 22,
122+
};
123+
return Err(std::io::Error::from_raw_os_error(errcode));
124+
}
123125

124126
Ok(SizedCache {
125127
store,
@@ -166,7 +168,7 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
166168
} = *self;
167169
// insert the value `index` at `hash`, the closure provided
168170
// is used to rehash values if a resize is necessary.
169-
store.insert(hash, index, move |&i| {
171+
store.insert_unique(hash, index, move |&i| {
170172
// rehash the "key" value stored at index `i` - requires looking
171173
// up the original "key" value in the `order` list.
172174
let hasher = &mut hash_builder.build_hasher();
@@ -186,7 +188,7 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
186188
// `key` value from the `order` list.
187189
// This pattern is repeated in other lookup situations.
188190
store
189-
.get(hash, |&i| key == order.get(i).0.borrow())
191+
.find(hash, |&i| key == order.get(i).0.borrow())
190192
.copied()
191193
}
192194

@@ -196,7 +198,10 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
196198
Q: std::hash::Hash + Eq + ?Sized,
197199
{
198200
let Self { store, order, .. } = self;
199-
store.remove_entry(hash, |&i| key == order.get(i).0.borrow())
201+
match store.find_entry(hash, |&i| key == order.get(i).0.borrow()) {
202+
Ok(entry) => Some(entry.remove().0),
203+
Err(_) => None,
204+
}
200205
}
201206

202207
fn check_capacity(&mut self) {
@@ -218,9 +223,14 @@ impl<K: Hash + Eq + Clone, V> SizedCache<K, V> {
218223
let hash = hasher.finish();
219224

220225
let order_ = &order;
221-
let erased = store.erase_entry(hash, |&i| *key == order_.get(i).0);
222-
assert!(erased, "SizedCache::cache_set failed evicting cache key");
223-
store.remove_entry(hash, |&i| *key == order_.get(i).0);
226+
match store.find_entry(hash, |&i| *key == order_.get(i).0) {
227+
Ok(entry) => {
228+
entry.remove();
229+
}
230+
Err(_) => {
231+
panic!("SizedCache::cache_set failed evicting cache key");
232+
}
233+
}
224234
order.remove(index);
225235
}
226236
}

0 commit comments

Comments
 (0)