diff --git a/src/lib.rs b/src/lib.rs index db9ef31..8fe7be4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1250,7 +1250,8 @@ impl LruCache { unsafe { Some((key.assume_init(), val.assume_init())) } } - /// Marks the key as the most recently used one. + /// Marks the key as the most recently used one. Returns true if the key + /// was promoted because it exists in the cache, false otherwise. /// /// # Example /// @@ -1269,10 +1270,13 @@ impl LruCache { /// // assert_eq!(cache.pop_lru(), Some((3, "c"))); /// /// // By promoting 3, we make sure it isn't popped. - /// cache.promote(&3); + /// assert!(cache.promote(&3)); /// assert_eq!(cache.pop_lru(), Some((1, "a"))); + /// + /// // Promoting an entry that doesn't exist doesn't do anything. + /// assert!(!cache.promote(&4)); /// ``` - pub fn promote(&mut self, k: &Q) + pub fn promote(&mut self, k: &Q) -> bool where K: Borrow, Q: Hash + Eq + ?Sized, @@ -1281,10 +1285,14 @@ impl LruCache { let node_ptr: *mut LruEntry = node.as_ptr(); self.detach(node_ptr); self.attach(node_ptr); + true + } else { + false } } - /// Marks the key as the least recently used one. + /// Marks the key as the least recently used one. Returns true if the key was demoted + /// because it exists in the cache, false otherwise. /// /// # Example /// @@ -1303,12 +1311,15 @@ impl LruCache { /// // assert_eq!(cache.pop_lru(), Some((3, "c"))); /// /// // By demoting 1 and 2, we make sure those are popped first. - /// cache.demote(&2); - /// cache.demote(&1); + /// assert!(cache.demote(&2)); + /// assert!(cache.demote(&1)); /// assert_eq!(cache.pop_lru(), Some((1, "a"))); /// assert_eq!(cache.pop_lru(), Some((2, "b"))); + /// + /// // Demoting a key that doesn't exist does nothing. + /// assert!(!cache.demote(&4)); /// ``` - pub fn demote(&mut self, k: &Q) + pub fn demote(&mut self, k: &Q) -> bool where K: Borrow, Q: Hash + Eq + ?Sized, @@ -1317,6 +1328,9 @@ impl LruCache { let node_ptr: *mut LruEntry = node.as_ptr(); self.detach(node_ptr); self.attach_last(node_ptr); + true + } else { + false } }