diff --git a/src/pool/arc.rs b/src/pool/arc.rs index 135163b64d..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,6 +219,21 @@ where &mut *ptr::addr_of_mut!((*this.node_ptr.as_ptr().cast::>()).data) } + /// 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::is_unique(this) { + // 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