Skip to content

Commit 3ab6993

Browse files
Merge pull request #4 from demoray/add-expiringmap-remove_entry
add ExpiringMap::remove_entry (and use it for ExpiringSet)
2 parents f8b0526 + 6a21d6e commit 3ab6993

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/lib.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ impl<K: PartialEq + Eq + Hash, V> ExpiringMap<K, V> {
252252
self.vacuum();
253253
self.inner.shrink_to(min_capacity);
254254
}
255+
256+
/// Removes a key from the map, returning the stored key and value if the key was previously in the map.
257+
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
258+
where
259+
K: Borrow<Q>,
260+
Q: ?Sized + Hash + Eq,
261+
{
262+
self.inner
263+
.remove_entry(key)
264+
.filter(|(_, v)| v.not_expired())
265+
.map(|(k, v)| (k, v.value))
266+
}
255267
}
256268

257269
impl<K: PartialEq + Eq + Hash> ExpiringSet<K> {
@@ -295,11 +307,7 @@ impl<K: PartialEq + Eq + Hash> ExpiringSet<K> {
295307
K: Borrow<Q>,
296308
Q: ?Sized + Hash + Eq,
297309
{
298-
self.0
299-
.inner
300-
.remove_entry(key)
301-
.filter(|(_, v)| v.not_expired())
302-
.map(|v| v.0)
310+
self.0.remove_entry(key).map(|(k, _)| k)
303311
}
304312

305313
/// Shrink the set to the minimum allowable size in accordance with the

0 commit comments

Comments
 (0)