From 4959eccd574ec24a5b3a9984304a9249d2d2a8ae Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Sun, 14 Feb 2021 18:02:15 +0000 Subject: [PATCH] Add strong/weak pointer counts to Shared (#2346) Co-authored-by: Harry Barber --- futures-util/src/future/future/shared.rs | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/futures-util/src/future/future/shared.rs b/futures-util/src/future/future/shared.rs index 53635b5582..5e6c88f194 100644 --- a/futures-util/src/future/future/shared.rs +++ b/futures-util/src/future/future/shared.rs @@ -126,6 +126,32 @@ where } None } + + /// Gets the number of strong pointers to this allocation. + /// + /// Returns [`None`] if it has already been polled to completion. + /// + /// # Safety + /// + /// This method by itself is safe, but using it correctly requires extra care. Another thread + /// can change the strong count at any time, including potentially between calling this method + /// and acting on the result. + pub fn strong_count(&self) -> Option { + self.inner.as_ref().map(|arc| Arc::strong_count(arc)) + } + + /// Gets the number of weak pointers to this allocation. + /// + /// Returns [`None`] if it has already been polled to completion. + /// + /// # Safety + /// + /// This method by itself is safe, but using it correctly requires extra care. Another thread + /// can change the weak count at any time, including potentially between calling this method + /// and acting on the result. + pub fn weak_count(&self) -> Option { + self.inner.as_ref().map(|arc| Arc::weak_count(arc)) + } } impl Inner