Skip to content

Commit

Permalink
Add Weak::atomic_count
Browse files Browse the repository at this point in the history
  • Loading branch information
EricLBuehler committed Sep 2, 2023
1 parent 648e586 commit 90935b7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,34 @@ impl<T> Weak<T> {
data: NonNull::from(Box::leak(sbx)),
}
}

/// Return the atomic reference count of the object. This is how many threads are using the data referenced by this `Weak<T>`.
///
/// # Examples
/// ```
/// use std::thread;
/// use trc::Trc;
/// use trc::SharedTrc;
/// use trc::Weak;
///
/// let trc = Trc::new(100);
/// let shared = SharedTrc::from_trc(&trc);
/// let weak = Trc::downgrade(&trc);
///
/// let handle = thread::spawn(move || {
/// assert_eq!(Weak::atomic_count(&weak), 2);
/// let trc = SharedTrc::to_trc(shared);
/// });
///
/// handle.join().unwrap();
/// assert_eq!(*trc, 100);
/// ```
#[inline]
pub fn atomic_count(this: &Self) -> usize {
unsafe { this.data.as_ref() }
.atomicref
.load(core::sync::atomic::Ordering::Relaxed)
}
}

impl<T: ?Sized> Clone for Weak<T> {
Expand Down

0 comments on commit 90935b7

Please sign in to comment.