From b664341d917cafade5a2470579a4c122fdcf941b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 29 Jan 2019 21:58:17 +0100 Subject: [PATCH] Make weak_count return an Option --- src/liballoc/rc.rs | 26 ++++++++++++-------------- src/liballoc/sync.rs | 22 ++++++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 7ee05eaff91..72b43b5d6ad 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1300,22 +1300,20 @@ pub fn strong_count(&self) -> usize { /// Gets the number of `Weak` pointers pointing to this value. /// - /// If `self` was created using [`Weak::new`], this will return 0. If not, - /// the returned value is at least 1, since `self` still points to the + /// If `self` was created using [`Weak::new`], this will return `None`. If + /// not, the returned value is at least 1, since `self` still points to the /// value. /// /// [`Weak::new`]: #method.new #[unstable(feature = "weak_counts", issue = "0")] - pub fn weak_count(&self) -> usize { - if let Some(inner) = self.inner() { + pub fn weak_count(&self) -> Option { + self.inner().map(|inner| { if inner.strong() > 0 { inner.weak() - 1 // subtract the implicit weak ptr } else { inner.weak() } - } else { - 0 - } + }) } /// Return `None` when the pointer is dangling and there is no allocated `RcBox`, @@ -1658,28 +1656,28 @@ fn test_weak_count() { #[test] fn weak_counts() { - assert_eq!(Weak::weak_count(&Weak::::new()), 0); + assert_eq!(Weak::weak_count(&Weak::::new()), None); assert_eq!(Weak::strong_count(&Weak::::new()), 0); let a = Rc::new(0); let w = Rc::downgrade(&a); assert_eq!(Weak::strong_count(&w), 1); - assert_eq!(Weak::weak_count(&w), 1); + assert_eq!(Weak::weak_count(&w), Some(1)); let w2 = w.clone(); assert_eq!(Weak::strong_count(&w), 1); - assert_eq!(Weak::weak_count(&w), 2); + assert_eq!(Weak::weak_count(&w), Some(2)); assert_eq!(Weak::strong_count(&w2), 1); - assert_eq!(Weak::weak_count(&w2), 2); + assert_eq!(Weak::weak_count(&w2), Some(2)); drop(w); assert_eq!(Weak::strong_count(&w2), 1); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); let a2 = a.clone(); assert_eq!(Weak::strong_count(&w2), 2); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); drop(a2); drop(a); assert_eq!(Weak::strong_count(&w2), 0); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); drop(w2); } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index b065bd60c60..777851e1925 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -1146,12 +1146,12 @@ pub fn strong_count(&self) -> usize { /// /// [`Weak::new`]: #method.new #[unstable(feature = "weak_counts", issue = "0")] - pub fn weak_count(&self) -> usize { + pub fn weak_count(&self) -> Option { // Due to the implicit weak pointer added when any strong pointers are // around, we cannot implement `weak_count` correctly since it // necessarily requires accessing the strong count and weak count in an // unsynchronized fashion. So this version is a bit racy. - if let Some(inner) = self.inner() { + self.inner().map(|inner| { let strong = inner.strong.load(SeqCst); let weak = inner.weak.load(SeqCst); if strong == 0 { @@ -1169,9 +1169,7 @@ pub fn weak_count(&self) -> usize { // pointer), we guard against that specifically. cmp::max(1, weak - 1) } - } else { - 0 - } + }) } /// Return `None` when the pointer is dangling and there is no allocated `ArcInner`, @@ -1695,28 +1693,28 @@ fn test_arc_get_mut() { #[test] fn weak_counts() { - assert_eq!(Weak::weak_count(&Weak::::new()), 0); + assert_eq!(Weak::weak_count(&Weak::::new()), None); assert_eq!(Weak::strong_count(&Weak::::new()), 0); let a = Arc::new(0); let w = Arc::downgrade(&a); assert_eq!(Weak::strong_count(&w), 1); - assert_eq!(Weak::weak_count(&w), 1); + assert_eq!(Weak::weak_count(&w), Some(1)); let w2 = w.clone(); assert_eq!(Weak::strong_count(&w), 1); - assert_eq!(Weak::weak_count(&w), 2); + assert_eq!(Weak::weak_count(&w), Some(2)); assert_eq!(Weak::strong_count(&w2), 1); - assert_eq!(Weak::weak_count(&w2), 2); + assert_eq!(Weak::weak_count(&w2), Some(2)); drop(w); assert_eq!(Weak::strong_count(&w2), 1); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); let a2 = a.clone(); assert_eq!(Weak::strong_count(&w2), 2); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); drop(a2); drop(a); assert_eq!(Weak::strong_count(&w2), 0); - assert_eq!(Weak::weak_count(&w2), 1); + assert_eq!(Weak::weak_count(&w2), Some(1)); drop(w2); } -- 2.44.0