Skip to content

Commit 14ea55b

Browse files
author
Christien Rioux
committed
lru with callback refactor
1 parent 7d09d96 commit 14ea55b

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

src/lru_cache.rs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,25 @@ where
131131
///
132132
/// If necessary, will remove the value at the front of the LRU list to make room.
133133
#[inline]
134-
pub fn insert<F: FnOnce(K, V)>(&mut self, k: K, v: V, remove_lru_callback: F) -> Option<V> {
134+
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
135+
let old_val = self.map.insert(k, v);
136+
if self.len() > self.capacity() {
137+
self.remove_lru();
138+
}
139+
old_val
140+
}
141+
142+
/// Insert a new value into the `LruCache` with LRU callback.
143+
///
144+
/// If necessary, will remove the value at the front of the LRU list to make room.
145+
/// Calls a callback if there was an LRU removed value
146+
#[inline]
147+
pub fn insert_with_callback<F: FnOnce(K, V)>(
148+
&mut self,
149+
k: K,
150+
v: V,
151+
remove_lru_callback: F,
152+
) -> Option<V> {
135153
let old_val = self.map.insert(k, v);
136154
if self.len() > self.capacity() {
137155
if let Some(x) = self.remove_lru() {
@@ -198,7 +216,28 @@ where
198216
/// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
199217
/// the LRU list.
200218
#[inline]
201-
pub fn entry<F: FnOnce(K, V)>(&mut self, key: K, remove_lru_callback: F) -> Entry<'_, K, V, S> {
219+
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S> {
220+
if self.len() > self.capacity() {
221+
self.remove_lru();
222+
}
223+
self.map.entry(key)
224+
}
225+
226+
/// Like `entry` but with LRU callback.
227+
/// If the returned entry is vacant, it will always have room to insert a single value. By
228+
/// using the entry API, you can exceed the configured capacity by 1.
229+
///
230+
/// The returned entry is not automatically moved to the back of the LRU list. By calling
231+
/// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
232+
/// the LRU list.
233+
/// Calls a callback if there was an LRU removed value
234+
235+
#[inline]
236+
pub fn entry_with_callback<F: FnOnce(K, V)>(
237+
&mut self,
238+
key: K,
239+
remove_lru_callback: F,
240+
) -> Entry<'_, K, V, S> {
202241
if self.len() > self.capacity() {
203242
if let Some(x) = self.remove_lru() {
204243
remove_lru_callback(x.0, x.1)
@@ -221,8 +260,25 @@ where
221260
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
222261
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
223262
/// entry in the LRU list.
263+
/// Calls a callback if there was an LRU removed value
264+
224265
#[inline]
225-
pub fn raw_entry_mut<F: FnOnce(K, V)>(
266+
pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S> {
267+
if self.len() > self.capacity() {
268+
self.remove_lru();
269+
}
270+
self.map.raw_entry_mut()
271+
}
272+
273+
/// Like `raw_entry` but with LRU callback.
274+
/// If the constructed raw entry is vacant, it will always have room to insert a single value.
275+
/// By using the raw entry API, you can exceed the configured capacity by 1.
276+
///
277+
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
278+
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
279+
/// entry in the LRU list.
280+
#[inline]
281+
pub fn raw_entry_mut_with_callback<F: FnOnce(K, V)>(
226282
&mut self,
227283
remove_lru_callback: F,
228284
) -> RawEntryBuilderMut<'_, K, V, S> {
@@ -257,7 +313,25 @@ where
257313
/// If there are more entries in the `LruCache` than the new capacity will allow, they are
258314
/// removed.
259315
#[inline]
260-
pub fn set_capacity<F: Fn(K, V)>(&mut self, capacity: usize, remove_lru_callback: F) {
316+
pub fn set_capacity(&mut self, capacity: usize) {
317+
for _ in capacity..self.len() {
318+
self.remove_lru();
319+
}
320+
self.max_size = capacity;
321+
}
322+
323+
/// Like `set_capacity` but with LRU callback.
324+
/// Set the new cache capacity for the `LruCache` with an LRU callback.
325+
///
326+
/// If there are more entries in the `LruCache` than the new capacity will allow, they are
327+
/// removed.
328+
/// Calls a callback if there was an LRU removed value
329+
#[inline]
330+
pub fn set_capacity_with_callback<F: Fn(K, V)>(
331+
&mut self,
332+
capacity: usize,
333+
remove_lru_callback: F,
334+
) {
261335
for _ in capacity..self.len() {
262336
if let Some(x) = self.remove_lru() {
263337
remove_lru_callback(x.0, x.1)

tests/serde.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![cfg(feature = "serde_impl")]
22

3-
use fxhash::FxBuildHasher;
43
use hashlink::{LinkedHashMap, LinkedHashSet, LruCache};
54
use rustc_hash::FxHasher;
65
use serde_test::{assert_tokens, Token};
@@ -175,7 +174,10 @@ fn lru_serde_tokens() {
175174

176175
#[test]
177176
fn lru_serde_tokens_empty_generic() {
178-
let map = LruCache::<char, u32, FxBuildHasher>::with_hasher(16, FxBuildHasher::default());
177+
let map = LruCache::<char, u32, BuildHasherDefault<FxHasher>>::with_hasher(
178+
16,
179+
BuildHasherDefault::<FxHasher>::default(),
180+
);
179181

180182
assert_tokens(
181183
&map,
@@ -196,7 +198,7 @@ fn lru_serde_tokens_empty_generic() {
196198

197199
#[test]
198200
fn lru_serde_tokens_generic() {
199-
let mut map = LruCache::with_hasher(16, FxBuildHasher::default());
201+
let mut map = LruCache::with_hasher(16, BuildHasherDefault::<FxHasher>::default());
200202
map.insert('a', 10);
201203
map.insert('b', 20);
202204
map.insert('c', 30);

0 commit comments

Comments
 (0)