Skip to content

Commit

Permalink
cc: add strong_count and weak_count exposing internal ref counts
Browse files Browse the repository at this point in the history
Match related std::rc::Rc APIs.
  • Loading branch information
quark-zju committed Jan 13, 2021
1 parent d1244d2 commit b95d96b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ impl<T: ?Sized, O: AbstractObjectSpace> RawCc<T, O> {
});
RawWeak(self.0)
}

/// Gets the reference count not considering weak references.
#[inline]
pub fn strong_count(&self) -> usize {
self.ref_count()
}
}

impl<T: ?Sized, O: AbstractObjectSpace> RawWeak<T, O> {
Expand All @@ -394,6 +400,18 @@ impl<T: ?Sized, O: AbstractObjectSpace> RawWeak<T, O> {
Some(RawCc(self.0))
}
}

/// Gets the reference count not considering weak references.
#[inline]
pub fn strong_count(&self) -> usize {
self.inner().ref_count()
}

/// Get the weak (non-owning) reference count.
#[inline]
pub fn weak_count(&self) -> usize {
self.inner().weak_count()
}
}

impl<T: ?Sized, O: AbstractObjectSpace> RawCc<T, O> {
Expand Down Expand Up @@ -428,8 +446,9 @@ impl<T: ?Sized, O: AbstractObjectSpace> RawCc<T, O> {
self.inner().ref_count()
}

/// Get the weak (non-owning) reference count.
#[inline]
fn weak_count(&self) -> usize {
pub fn weak_count(&self) -> usize {
self.inner().weak_count()
}

Expand Down
16 changes: 16 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ fn test_weakref_without_cycles() {
let w1 = s1.downgrade();
let s2 = w1.upgrade().unwrap();
let w2 = w1.clone();
assert_eq!(s2.strong_count(), 2);
assert_eq!(s2.weak_count(), 2);
assert_eq!(w2.strong_count(), 2);
assert_eq!(w2.weak_count(), 2);
drop(s1);
drop(s2);
let w3 = w2.clone();
assert!(w3.upgrade().is_none());
assert!(w2.upgrade().is_none());
assert!(w1.upgrade().is_none());
assert_eq!(w3.strong_count(), 0);
assert_eq!(w3.weak_count(), 3);
});
assert_eq!(
log,
Expand All @@ -135,13 +141,21 @@ fn test_weakref_with_cycles() {
let log = debug::capture_log(|| {
debug::NEXT_DEBUG_NAME.with(|n| n.set(1));
let a: Cc<RefCell<Vec<Box<dyn Trace>>>> = Cc::new(RefCell::new(Vec::new()));
assert_eq!(a.strong_count(), 1);
debug::NEXT_DEBUG_NAME.with(|n| n.set(2));
let b: Cc<RefCell<Vec<Box<dyn Trace>>>> = Cc::new(RefCell::new(Vec::new()));
a.borrow_mut().push(Box::new(b.clone()));
b.borrow_mut().push(Box::new(a.clone()));
assert_eq!(a.strong_count(), 2);
assert_eq!(a.weak_count(), 0);
let wa = a.downgrade();
assert_eq!(a.weak_count(), 1);
let wa1 = wa.clone();
assert_eq!(a.weak_count(), 2);
let wb = b.downgrade();
assert_eq!(wa.strong_count(), 2);
assert_eq!(wa.weak_count(), 2);
assert_eq!(wb.weak_count(), 1);
drop(a);
drop(b);
assert!(wa.upgrade().is_some());
Expand All @@ -151,6 +165,8 @@ fn test_weakref_with_cycles() {
assert!(wa1.upgrade().is_none());
assert!(wb.upgrade().is_none());
assert!(wb.clone().upgrade().is_none());
assert_eq!(wa.weak_count(), 2);
assert_eq!(wa.strong_count(), 0);
});
assert_eq!(
log,
Expand Down

0 comments on commit b95d96b

Please sign in to comment.