From 1df16c0394e4168927c9475884b93f6bd1b4b53f Mon Sep 17 00:00:00 2001 From: "Michael P. Jung" Date: Mon, 9 Dec 2024 16:53:05 +0000 Subject: [PATCH] Change retain() function to accept a FnMut This closes #370 --- src/managed/mod.rs | 2 +- tests/managed.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/managed/mod.rs b/src/managed/mod.rs index 3c35ade..c7dd35f 100644 --- a/src/managed/mod.rs +++ b/src/managed/mod.rs @@ -519,7 +519,7 @@ impl>> Pool { /// } /// }); /// ``` - 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| { diff --git a/tests/managed.rs b/tests/managed.rs index adf8613..3aeb750 100644 --- a/tests/managed.rs +++ b/tests/managed.rs @@ -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 + }); + } +}