@@ -1250,7 +1250,8 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
1250
1250
unsafe { Some ( ( key. assume_init ( ) , val. assume_init ( ) ) ) }
1251
1251
}
1252
1252
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.
1254
1255
///
1255
1256
/// # Example
1256
1257
///
@@ -1269,10 +1270,13 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
1269
1270
/// // assert_eq!(cache.pop_lru(), Some((3, "c")));
1270
1271
///
1271
1272
/// // By promoting 3, we make sure it isn't popped.
1272
- /// cache.promote(&3);
1273
+ /// assert!( cache.promote(&3) );
1273
1274
/// 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));
1274
1278
/// ```
1275
- pub fn promote < Q > ( & mut self , k : & Q )
1279
+ pub fn promote < Q > ( & mut self , k : & Q ) -> bool
1276
1280
where
1277
1281
K : Borrow < Q > ,
1278
1282
Q : Hash + Eq + ?Sized ,
@@ -1281,10 +1285,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
1281
1285
let node_ptr: * mut LruEntry < K , V > = node. as_ptr ( ) ;
1282
1286
self . detach ( node_ptr) ;
1283
1287
self . attach ( node_ptr) ;
1288
+ true
1289
+ } else {
1290
+ false
1284
1291
}
1285
1292
}
1286
1293
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.
1288
1296
///
1289
1297
/// # Example
1290
1298
///
@@ -1303,12 +1311,15 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
1303
1311
/// // assert_eq!(cache.pop_lru(), Some((3, "c")));
1304
1312
///
1305
1313
/// // 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) );
1308
1316
/// assert_eq!(cache.pop_lru(), Some((1, "a")));
1309
1317
/// assert_eq!(cache.pop_lru(), Some((2, "b")));
1318
+ ///
1319
+ /// // Demoting a key that doesn't exist does nothing.
1320
+ /// assert!(!cache.demote(&4));
1310
1321
/// ```
1311
- pub fn demote < Q > ( & mut self , k : & Q )
1322
+ pub fn demote < Q > ( & mut self , k : & Q ) -> bool
1312
1323
where
1313
1324
K : Borrow < Q > ,
1314
1325
Q : Hash + Eq + ?Sized ,
@@ -1317,6 +1328,9 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
1317
1328
let node_ptr: * mut LruEntry < K , V > = node. as_ptr ( ) ;
1318
1329
self . detach ( node_ptr) ;
1319
1330
self . attach_last ( node_ptr) ;
1331
+ true
1332
+ } else {
1333
+ false
1320
1334
}
1321
1335
}
1322
1336
0 commit comments