Skip to content

Commit 600833d

Browse files
committed
feat: Arc strong_count and get_mut public functions
1 parent d29f95c commit 600833d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/pool/arc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ where
218218
&mut *ptr::addr_of_mut!((*this.node_ptr.as_ptr().cast::<ArcInner<P::Data>>()).data)
219219
}
220220

221+
/// Returns the number of strong (`Arc`) pointers to this allocation
222+
pub fn strong_count(this: &Self) -> usize {
223+
this.inner().strong.load(Ordering::SeqCst)
224+
}
225+
226+
/// Returns a mutable reference to the inner data if there are no other `Arc` pointers to the
227+
pub fn get_mut(this: &mut Self) -> Option<&mut P::Data> {
228+
if Self::strong_count(this) == 1 {
229+
// SAFETY: we just checked that the strong count is 1
230+
Some(unsafe { Self::get_mut_unchecked(this) })
231+
} else {
232+
None
233+
}
234+
}
235+
221236
#[inline(never)]
222237
unsafe fn drop_slow(&mut self) {
223238
// run `P::Data`'s destructor

0 commit comments

Comments
 (0)