diff options
| author | Jonas Schievink <jonasschievink@gmail.com> | 2019-01-29 21:58:17 +0100 |
|---|---|---|
| committer | Jonas Schievink <jonasschievink@gmail.com> | 2019-01-29 21:58:17 +0100 |
| commit | b664341d917cafade5a2470579a4c122fdcf941b (patch) | |
| tree | 8307da96f6597dc0d8a534bf49ba37bddcda6b0f | |
| parent | 7e0edb39baa02c58c373f05014ffd8e3058c93ad (diff) | |
| download | rust-b664341d917cafade5a2470579a4c122fdcf941b.tar.gz rust-b664341d917cafade5a2470579a4c122fdcf941b.zip | |
Make weak_count return an Option<usize>
| -rw-r--r-- | src/liballoc/rc.rs | 26 | ||||
| -rw-r--r-- | 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 @@ impl<T: ?Sized> Weak<T> { /// 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<usize> { + 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 @@ mod tests { #[test] fn weak_counts() { - assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0); + assert_eq!(Weak::weak_count(&Weak::<u64>::new()), None); assert_eq!(Weak::strong_count(&Weak::<u64>::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 @@ impl<T: ?Sized> Weak<T> { /// /// [`Weak::new`]: #method.new #[unstable(feature = "weak_counts", issue = "0")] - pub fn weak_count(&self) -> usize { + pub fn weak_count(&self) -> Option<usize> { // 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 @@ impl<T: ?Sized> Weak<T> { // 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 @@ mod tests { #[test] fn weak_counts() { - assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0); + assert_eq!(Weak::weak_count(&Weak::<u64>::new()), None); assert_eq!(Weak::strong_count(&Weak::<u64>::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); } |
