@@ -958,6 +958,33 @@ impl<T: ?Sized> RefCell<T> {
958958 unsafe { & mut * self . value . get ( ) }
959959 }
960960
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+
961988 /// Immutably borrows the wrapped value, returning an error if the value is
962989 /// currently mutably borrowed.
963990 ///
@@ -1272,8 +1299,10 @@ impl<'b, T: ?Sized> Ref<'b, T> {
12721299 /// ```
12731300 #[ unstable( feature = "cell_leak" , issue = "69099" ) ]
12741301 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.
12771306 mem:: forget ( orig. borrow ) ;
12781307 orig. value
12791308 }
@@ -1387,9 +1416,11 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
13871416 /// ```
13881417 #[ unstable( feature = "cell_leak" , issue = "69099" ) ]
13891418 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.
13931424 mem:: forget ( orig. borrow ) ;
13941425 orig. value
13951426 }
0 commit comments