diff options
| author | bors <bors@rust-lang.org> | 2020-03-15 17:27:47 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-03-15 17:27:47 +0000 |
| commit | e0f5df017368dc3f7cb458fc6d5a5e0420e1d2e5 (patch) | |
| tree | 9c345daf32ba2b8a08337ccac954bb9ae905ca48 /src/libcore | |
| parent | 5da2e53f471f5eea60f534845b90c97ecabb71cb (diff) | |
| parent | bde77af0944e7b6f666b64f92bc30f8fd55296d2 (diff) | |
| download | rust-e0f5df017368dc3f7cb458fc6d5a5e0420e1d2e5.tar.gz rust-e0f5df017368dc3f7cb458fc6d5a5e0420e1d2e5.zip | |
Auto merge of #70024 - Centril:rollup-cppmaxr, r=Centril
Rollup of 8 pull requests Successful merges: - #69528 (Add undo_leak to reset RefCell borrow state) - #69589 (ast: `Mac`/`Macro` -> `MacCall`) - #69661 (Implement From<&mut str> for String) - #69988 (rustc_metadata: Remove `rmeta::MacroDef`) - #70006 (resolve: Fix two issues in fresh binding disambiguation) - #70011 (def_collector: Fully visit async functions) - #70013 (Return feature gate as a `Symbol` ) - #70018 (Fix "since" field for `Once::is_complete`'s `#[stable]` attribute) Failed merges: r? @ghost
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cell.rs | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 9ebb3176418..dbf073cd479 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -958,6 +958,33 @@ impl<T: ?Sized> RefCell<T> { unsafe { &mut *self.value.get() } } + /// Undo the effect of leaked guards on the borrow state of the `RefCell`. + /// + /// This call is similar to [`get_mut`] but more specialized. It borrows `RefCell` mutably to + /// ensure no borrows exist and then resets the state tracking shared borrows. This is relevant + /// if some `Ref` or `RefMut` borrows have been leaked. + /// + /// [`get_mut`]: #method.get_mut + /// + /// # Examples + /// + /// ``` + /// #![feature(cell_leak)] + /// use std::cell::RefCell; + /// + /// let mut c = RefCell::new(0); + /// std::mem::forget(c.borrow_mut()); + /// + /// assert!(c.try_borrow().is_err()); + /// c.undo_leak(); + /// assert!(c.try_borrow().is_ok()); + /// ``` + #[unstable(feature = "cell_leak", issue = "69099")] + pub fn undo_leak(&mut self) -> &mut T { + *self.borrow.get_mut() = UNUSED; + self.get_mut() + } + /// Immutably borrows the wrapped value, returning an error if the value is /// currently mutably borrowed. /// @@ -1272,8 +1299,10 @@ impl<'b, T: ?Sized> Ref<'b, T> { /// ``` #[unstable(feature = "cell_leak", issue = "69099")] pub fn leak(orig: Ref<'b, T>) -> &'b T { - // By forgetting this Ref we ensure that the borrow counter in the RefCell never goes back - // to UNUSED again. No further mutable references can be created from the original cell. + // By forgetting this Ref we ensure that the borrow counter in the RefCell can't go back to + // UNUSED within the lifetime `'b`. Resetting the reference tracking state would require a + // unique reference to the borrowed RefCell. No further mutable references can be created + // from the original cell. mem::forget(orig.borrow); orig.value } @@ -1387,9 +1416,11 @@ impl<'b, T: ?Sized> RefMut<'b, T> { /// ``` #[unstable(feature = "cell_leak", issue = "69099")] pub fn leak(orig: RefMut<'b, T>) -> &'b mut T { - // By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell never - // goes back to UNUSED again. No further references can be created from the original cell, - // making the current borrow the only reference for the remaining lifetime. + // By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell can't + // go back to UNUSED within the lifetime `'b`. Resetting the reference tracking state would + // require a unique reference to the borrowed RefCell. No further references can be created + // from the original cell within that lifetime, making the current borrow the only + // reference for the remaining lifetime. mem::forget(orig.borrow); orig.value } |
