Skip to content

Commit

Permalink
Change retain() function to accept a FnMut
Browse files Browse the repository at this point in the history
This closes #370
  • Loading branch information
bikeshedder committed Dec 18, 2024
1 parent b438af3 commit 1df16c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/managed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl<M: Manager, W: From<Object<M>>> Pool<M, W> {
/// }
/// });
/// ```
pub fn retain(&self, f: impl Fn(&M::Type, Metrics) -> bool) {
pub fn retain(&self, mut f: impl FnMut(&M::Type, Metrics) -> bool) {
let mut guard = self.inner.slots.lock().unwrap();
let len_before = guard.vec.len();
guard.vec.retain_mut(|obj| {
Expand Down
19 changes: 19 additions & 0 deletions tests/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,22 @@ async fn retain() {
pool.retain(|_, metrics| metrics.age() <= Duration::from_millis(10));
assert_eq!(pool.status().size, 0);
}

#[tokio::test]
async fn retain_fnmut() {
let mgr = Manager {};
let pool = Pool::builder(mgr).max_size(4).build().unwrap();
{
let _a = pool.get().await;
let _b = pool.get().await;
let _c = pool.get().await;
let _c = pool.get().await;
}
let mut removed = 0;
{
pool.retain(|_, _| {
removed += 1;
false
});
}
}

0 comments on commit 1df16c0

Please sign in to comment.