Skip to content

Commit b664341

Browse files
Make weak_count return an Option<usize>
1 parent 7e0edb3 commit b664341

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/liballoc/rc.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,22 +1300,20 @@ impl<T: ?Sized> Weak<T> {
13001300

13011301
/// Gets the number of `Weak` pointers pointing to this value.
13021302
///
1303-
/// If `self` was created using [`Weak::new`], this will return 0. If not,
1304-
/// the returned value is at least 1, since `self` still points to the
1303+
/// If `self` was created using [`Weak::new`], this will return `None`. If
1304+
/// not, the returned value is at least 1, since `self` still points to the
13051305
/// value.
13061306
///
13071307
/// [`Weak::new`]: #method.new
13081308
#[unstable(feature = "weak_counts", issue = "0")]
1309-
pub fn weak_count(&self) -> usize {
1310-
if let Some(inner) = self.inner() {
1309+
pub fn weak_count(&self) -> Option<usize> {
1310+
self.inner().map(|inner| {
13111311
if inner.strong() > 0 {
13121312
inner.weak() - 1 // subtract the implicit weak ptr
13131313
} else {
13141314
inner.weak()
13151315
}
1316-
} else {
1317-
0
1318-
}
1316+
})
13191317
}
13201318

13211319
/// Return `None` when the pointer is dangling and there is no allocated `RcBox`,
@@ -1658,28 +1656,28 @@ mod tests {
16581656

16591657
#[test]
16601658
fn weak_counts() {
1661-
assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0);
1659+
assert_eq!(Weak::weak_count(&Weak::<u64>::new()), None);
16621660
assert_eq!(Weak::strong_count(&Weak::<u64>::new()), 0);
16631661

16641662
let a = Rc::new(0);
16651663
let w = Rc::downgrade(&a);
16661664
assert_eq!(Weak::strong_count(&w), 1);
1667-
assert_eq!(Weak::weak_count(&w), 1);
1665+
assert_eq!(Weak::weak_count(&w), Some(1));
16681666
let w2 = w.clone();
16691667
assert_eq!(Weak::strong_count(&w), 1);
1670-
assert_eq!(Weak::weak_count(&w), 2);
1668+
assert_eq!(Weak::weak_count(&w), Some(2));
16711669
assert_eq!(Weak::strong_count(&w2), 1);
1672-
assert_eq!(Weak::weak_count(&w2), 2);
1670+
assert_eq!(Weak::weak_count(&w2), Some(2));
16731671
drop(w);
16741672
assert_eq!(Weak::strong_count(&w2), 1);
1675-
assert_eq!(Weak::weak_count(&w2), 1);
1673+
assert_eq!(Weak::weak_count(&w2), Some(1));
16761674
let a2 = a.clone();
16771675
assert_eq!(Weak::strong_count(&w2), 2);
1678-
assert_eq!(Weak::weak_count(&w2), 1);
1676+
assert_eq!(Weak::weak_count(&w2), Some(1));
16791677
drop(a2);
16801678
drop(a);
16811679
assert_eq!(Weak::strong_count(&w2), 0);
1682-
assert_eq!(Weak::weak_count(&w2), 1);
1680+
assert_eq!(Weak::weak_count(&w2), Some(1));
16831681
drop(w2);
16841682
}
16851683

src/liballoc/sync.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,12 @@ impl<T: ?Sized> Weak<T> {
11461146
///
11471147
/// [`Weak::new`]: #method.new
11481148
#[unstable(feature = "weak_counts", issue = "0")]
1149-
pub fn weak_count(&self) -> usize {
1149+
pub fn weak_count(&self) -> Option<usize> {
11501150
// Due to the implicit weak pointer added when any strong pointers are
11511151
// around, we cannot implement `weak_count` correctly since it
11521152
// necessarily requires accessing the strong count and weak count in an
11531153
// unsynchronized fashion. So this version is a bit racy.
1154-
if let Some(inner) = self.inner() {
1154+
self.inner().map(|inner| {
11551155
let strong = inner.strong.load(SeqCst);
11561156
let weak = inner.weak.load(SeqCst);
11571157
if strong == 0 {
@@ -1169,9 +1169,7 @@ impl<T: ?Sized> Weak<T> {
11691169
// pointer), we guard against that specifically.
11701170
cmp::max(1, weak - 1)
11711171
}
1172-
} else {
1173-
0
1174-
}
1172+
})
11751173
}
11761174

11771175
/// Return `None` when the pointer is dangling and there is no allocated `ArcInner`,
@@ -1695,28 +1693,28 @@ mod tests {
16951693

16961694
#[test]
16971695
fn weak_counts() {
1698-
assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0);
1696+
assert_eq!(Weak::weak_count(&Weak::<u64>::new()), None);
16991697
assert_eq!(Weak::strong_count(&Weak::<u64>::new()), 0);
17001698

17011699
let a = Arc::new(0);
17021700
let w = Arc::downgrade(&a);
17031701
assert_eq!(Weak::strong_count(&w), 1);
1704-
assert_eq!(Weak::weak_count(&w), 1);
1702+
assert_eq!(Weak::weak_count(&w), Some(1));
17051703
let w2 = w.clone();
17061704
assert_eq!(Weak::strong_count(&w), 1);
1707-
assert_eq!(Weak::weak_count(&w), 2);
1705+
assert_eq!(Weak::weak_count(&w), Some(2));
17081706
assert_eq!(Weak::strong_count(&w2), 1);
1709-
assert_eq!(Weak::weak_count(&w2), 2);
1707+
assert_eq!(Weak::weak_count(&w2), Some(2));
17101708
drop(w);
17111709
assert_eq!(Weak::strong_count(&w2), 1);
1712-
assert_eq!(Weak::weak_count(&w2), 1);
1710+
assert_eq!(Weak::weak_count(&w2), Some(1));
17131711
let a2 = a.clone();
17141712
assert_eq!(Weak::strong_count(&w2), 2);
1715-
assert_eq!(Weak::weak_count(&w2), 1);
1713+
assert_eq!(Weak::weak_count(&w2), Some(1));
17161714
drop(a2);
17171715
drop(a);
17181716
assert_eq!(Weak::strong_count(&w2), 0);
1719-
assert_eq!(Weak::weak_count(&w2), 1);
1717+
assert_eq!(Weak::weak_count(&w2), Some(1));
17201718
drop(w2);
17211719
}
17221720

0 commit comments

Comments
 (0)