From 600833ddde6e720c3d751fdd4aaf7b4b458acb44 Mon Sep 17 00:00:00 2001 From: Enzo Le Van Date: Wed, 17 Sep 2025 13:01:34 +0200 Subject: [PATCH 1/2] feat: Arc strong_count and get_mut public functions --- src/pool/arc.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/pool/arc.rs b/src/pool/arc.rs index 135163b64d..5fdcfe1591 100644 --- a/src/pool/arc.rs +++ b/src/pool/arc.rs @@ -218,6 +218,21 @@ where &mut *ptr::addr_of_mut!((*this.node_ptr.as_ptr().cast::>()).data) } + /// Returns the number of strong (`Arc`) pointers to this allocation + pub fn strong_count(this: &Self) -> usize { + this.inner().strong.load(Ordering::SeqCst) + } + + /// Returns a mutable reference to the inner data if there are no other `Arc` pointers to the + pub fn get_mut(this: &mut Self) -> Option<&mut P::Data> { + if Self::strong_count(this) == 1 { + // SAFETY: we just checked that the strong count is 1 + Some(unsafe { Self::get_mut_unchecked(this) }) + } else { + None + } + } + #[inline(never)] unsafe fn drop_slow(&mut self) { // run `P::Data`'s destructor From 9aac2bd36690c2e39f4f40e1c53bd9fe83a2ea6e Mon Sep 17 00:00:00 2001 From: Enzo Le Van Date: Wed, 17 Sep 2025 16:25:51 +0200 Subject: [PATCH 2/2] correct strong count acquisition for get_mut --- src/pool/arc.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/pool/arc.rs b/src/pool/arc.rs index 5fdcfe1591..7147d5b777 100644 --- a/src/pool/arc.rs +++ b/src/pool/arc.rs @@ -73,7 +73,8 @@ use core::{ fmt, hash::{Hash, Hasher}, mem::{ManuallyDrop, MaybeUninit}, - ops, ptr, + ops, + ptr::{self}, }; #[cfg(not(feature = "portable-atomic"))] @@ -218,14 +219,14 @@ where &mut *ptr::addr_of_mut!((*this.node_ptr.as_ptr().cast::>()).data) } - /// Returns the number of strong (`Arc`) pointers to this allocation - pub fn strong_count(this: &Self) -> usize { - this.inner().strong.load(Ordering::SeqCst) + /// Returns `true` if there are no other `Arc` pointers to the same allocation + pub fn is_unique(this: &Self) -> bool { + this.inner().strong.load(Ordering::Acquire) == 1 } /// Returns a mutable reference to the inner data if there are no other `Arc` pointers to the pub fn get_mut(this: &mut Self) -> Option<&mut P::Data> { - if Self::strong_count(this) == 1 { + if Self::is_unique(this) { // SAFETY: we just checked that the strong count is 1 Some(unsafe { Self::get_mut_unchecked(this) }) } else {