Skip to content

Commit 9903725

Browse files
authored
Merge pull request #212 from levkk/levkk-promote
Return true/false for promote & demote
2 parents 5ec44f5 + 11d5b77 commit 9903725

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/lib.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,8 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
12501250
unsafe { Some((key.assume_init(), val.assume_init())) }
12511251
}
12521252

1253-
/// Marks the key as the most recently used one.
1253+
/// Marks the key as the most recently used one. Returns true if the key
1254+
/// was promoted because it exists in the cache, false otherwise.
12541255
///
12551256
/// # Example
12561257
///
@@ -1269,10 +1270,13 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
12691270
/// // assert_eq!(cache.pop_lru(), Some((3, "c")));
12701271
///
12711272
/// // By promoting 3, we make sure it isn't popped.
1272-
/// cache.promote(&3);
1273+
/// assert!(cache.promote(&3));
12731274
/// assert_eq!(cache.pop_lru(), Some((1, "a")));
1275+
///
1276+
/// // Promoting an entry that doesn't exist doesn't do anything.
1277+
/// assert!(!cache.promote(&4));
12741278
/// ```
1275-
pub fn promote<Q>(&mut self, k: &Q)
1279+
pub fn promote<Q>(&mut self, k: &Q) -> bool
12761280
where
12771281
K: Borrow<Q>,
12781282
Q: Hash + Eq + ?Sized,
@@ -1281,10 +1285,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
12811285
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
12821286
self.detach(node_ptr);
12831287
self.attach(node_ptr);
1288+
true
1289+
} else {
1290+
false
12841291
}
12851292
}
12861293

1287-
/// Marks the key as the least recently used one.
1294+
/// Marks the key as the least recently used one. Returns true if the key was demoted
1295+
/// because it exists in the cache, false otherwise.
12881296
///
12891297
/// # Example
12901298
///
@@ -1303,12 +1311,15 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
13031311
/// // assert_eq!(cache.pop_lru(), Some((3, "c")));
13041312
///
13051313
/// // By demoting 1 and 2, we make sure those are popped first.
1306-
/// cache.demote(&2);
1307-
/// cache.demote(&1);
1314+
/// assert!(cache.demote(&2));
1315+
/// assert!(cache.demote(&1));
13081316
/// assert_eq!(cache.pop_lru(), Some((1, "a")));
13091317
/// assert_eq!(cache.pop_lru(), Some((2, "b")));
1318+
///
1319+
/// // Demoting a key that doesn't exist does nothing.
1320+
/// assert!(!cache.demote(&4));
13101321
/// ```
1311-
pub fn demote<Q>(&mut self, k: &Q)
1322+
pub fn demote<Q>(&mut self, k: &Q) -> bool
13121323
where
13131324
K: Borrow<Q>,
13141325
Q: Hash + Eq + ?Sized,
@@ -1317,6 +1328,9 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
13171328
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
13181329
self.detach(node_ptr);
13191330
self.attach_last(node_ptr);
1331+
true
1332+
} else {
1333+
false
13201334
}
13211335
}
13221336

0 commit comments

Comments
 (0)