@@ -958,6 +958,33 @@ impl<T: ?Sized> RefCell<T> {
958
958
unsafe { & mut * self . value . get ( ) }
959
959
}
960
960
961
+ /// Undo the effect of leaked guards on the borrow state of the `RefCell`.
962
+ ///
963
+ /// This call is similar to [`get_mut`] but more specialized. It borrows `RefCell` mutably to
964
+ /// ensure no borrows exist and then resets the state tracking shared borrows. This is relevant
965
+ /// if some `Ref` or `RefMut` borrows have been leaked.
966
+ ///
967
+ /// [`get_mut`]: #method.get_mut
968
+ ///
969
+ /// # Examples
970
+ ///
971
+ /// ```
972
+ /// #![feature(cell_leak)]
973
+ /// use std::cell::RefCell;
974
+ ///
975
+ /// let mut c = RefCell::new(0);
976
+ /// std::mem::forget(c.borrow_mut());
977
+ ///
978
+ /// assert!(c.try_borrow().is_err());
979
+ /// c.undo_leak();
980
+ /// assert!(c.try_borrow().is_ok());
981
+ /// ```
982
+ #[ unstable( feature = "cell_leak" , issue = "69099" ) ]
983
+ pub fn undo_leak ( & mut self ) -> & mut T {
984
+ * self . borrow . get_mut ( ) = UNUSED ;
985
+ self . get_mut ( )
986
+ }
987
+
961
988
/// Immutably borrows the wrapped value, returning an error if the value is
962
989
/// currently mutably borrowed.
963
990
///
@@ -1272,8 +1299,10 @@ impl<'b, T: ?Sized> Ref<'b, T> {
1272
1299
/// ```
1273
1300
#[ unstable( feature = "cell_leak" , issue = "69099" ) ]
1274
1301
pub fn leak ( orig : Ref < ' b , T > ) -> & ' b T {
1275
- // By forgetting this Ref we ensure that the borrow counter in the RefCell never goes back
1276
- // to UNUSED again. No further mutable references can be created from the original cell.
1302
+ // By forgetting this Ref we ensure that the borrow counter in the RefCell can't go back to
1303
+ // UNUSED within the lifetime `'b`. Resetting the reference tracking state would require a
1304
+ // unique reference to the borrowed RefCell. No further mutable references can be created
1305
+ // from the original cell.
1277
1306
mem:: forget ( orig. borrow ) ;
1278
1307
orig. value
1279
1308
}
@@ -1387,9 +1416,11 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
1387
1416
/// ```
1388
1417
#[ unstable( feature = "cell_leak" , issue = "69099" ) ]
1389
1418
pub fn leak ( orig : RefMut < ' b , T > ) -> & ' b mut T {
1390
- // By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell never
1391
- // goes back to UNUSED again. No further references can be created from the original cell,
1392
- // making the current borrow the only reference for the remaining lifetime.
1419
+ // By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell can't
1420
+ // go back to UNUSED within the lifetime `'b`. Resetting the reference tracking state would
1421
+ // require a unique reference to the borrowed RefCell. No further references can be created
1422
+ // from the original cell within that lifetime, making the current borrow the only
1423
+ // reference for the remaining lifetime.
1393
1424
mem:: forget ( orig. borrow ) ;
1394
1425
orig. value
1395
1426
}
0 commit comments