@@ -1284,6 +1284,40 @@ impl<T: ?Sized> Weak<T> {
1284
1284
}
1285
1285
}
1286
1286
1287
+ /// Gets the number of strong (`Rc`) pointers pointing to this value.
1288
+ ///
1289
+ /// If `self` was created using [`Weak::new`], this will return 0.
1290
+ ///
1291
+ /// [`Weak::new`]: #method.new
1292
+ #[ unstable( feature = "weak_counts" , issue = "0" ) ]
1293
+ pub fn strong_count ( & self ) -> usize {
1294
+ if let Some ( inner) = self . inner ( ) {
1295
+ inner. strong ( )
1296
+ } else {
1297
+ 0
1298
+ }
1299
+ }
1300
+
1301
+ /// Gets the number of `Weak` pointers pointing to this value.
1302
+ ///
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
1305
+ /// value.
1306
+ ///
1307
+ /// [`Weak::new`]: #method.new
1308
+ #[ unstable( feature = "weak_counts" , issue = "0" ) ]
1309
+ pub fn weak_count ( & self ) -> usize {
1310
+ if let Some ( inner) = self . inner ( ) {
1311
+ if inner. strong ( ) > 0 {
1312
+ inner. weak ( ) - 1 // subtract the implicit weak ptr
1313
+ } else {
1314
+ inner. weak ( )
1315
+ }
1316
+ } else {
1317
+ 0
1318
+ }
1319
+ }
1320
+
1287
1321
/// Return `None` when the pointer is dangling and there is no allocated `RcBox`,
1288
1322
/// i.e., this `Weak` was created by `Weak::new`
1289
1323
#[ inline]
@@ -1622,6 +1656,33 @@ mod tests {
1622
1656
drop ( c) ;
1623
1657
}
1624
1658
1659
+ #[ test]
1660
+ fn weak_counts ( ) {
1661
+ assert_eq ! ( Weak :: weak_count( & Weak :: <u64 >:: new( ) ) , 0 ) ;
1662
+ assert_eq ! ( Weak :: strong_count( & Weak :: <u64 >:: new( ) ) , 0 ) ;
1663
+
1664
+ let a = Rc :: new ( 0 ) ;
1665
+ let w = Rc :: downgrade ( & a) ;
1666
+ assert_eq ! ( Weak :: strong_count( & w) , 1 ) ;
1667
+ assert_eq ! ( Weak :: weak_count( & w) , 1 ) ;
1668
+ let w2 = w. clone ( ) ;
1669
+ assert_eq ! ( Weak :: strong_count( & w) , 1 ) ;
1670
+ assert_eq ! ( Weak :: weak_count( & w) , 2 ) ;
1671
+ assert_eq ! ( Weak :: strong_count( & w2) , 1 ) ;
1672
+ assert_eq ! ( Weak :: weak_count( & w2) , 2 ) ;
1673
+ drop ( w) ;
1674
+ assert_eq ! ( Weak :: strong_count( & w2) , 1 ) ;
1675
+ assert_eq ! ( Weak :: weak_count( & w2) , 1 ) ;
1676
+ let a2 = a. clone ( ) ;
1677
+ assert_eq ! ( Weak :: strong_count( & w2) , 2 ) ;
1678
+ assert_eq ! ( Weak :: weak_count( & w2) , 1 ) ;
1679
+ drop ( a2) ;
1680
+ drop ( a) ;
1681
+ assert_eq ! ( Weak :: strong_count( & w2) , 0 ) ;
1682
+ assert_eq ! ( Weak :: weak_count( & w2) , 1 ) ;
1683
+ drop ( w2) ;
1684
+ }
1685
+
1625
1686
#[ test]
1626
1687
fn try_unwrap ( ) {
1627
1688
let x = Rc :: new ( 3 ) ;
0 commit comments