Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drop_leak and drop_leak_mut #83786

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,52 @@ impl<T: ?Sized> RefCell<T> {
self.get_mut()
}

/// Assert that a single leaked immutable guard has been cleaned up.
///
/// # Examples
///
/// ```
/// #![feature(cell_leak)]
/// use std::cell::{RefCell, Ref};
///
/// let mut c = RefCell::new(0);
/// Ref::leak(c.borrow());
///
/// assert!(c.try_borrow_mut().is_err());
/// unsafe { c.drop_leak() };
/// assert!(c.try_borrow_mut().is_ok());
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a panic section to describe when this panics.

#[unstable(feature = "cell_leak", issue = "69099")]
pub unsafe fn drop_leak(&self) {
self.borrow.update(|x| {
assert!(x > 0, "Cell not borrowed immutably");
x - 1
});
}

/// Assert that a single leaked mutable guard has been cleaned up.
///
/// # Examples
///
/// ```
/// #![feature(cell_leak)]
/// use std::cell::{RefCell, RefMut};
///
/// let mut c = RefCell::new(0);
/// RefMut::leak(c.borrow_mut());
///
/// assert!(c.try_borrow().is_err());
/// unsafe { c.drop_leak_mut() };
/// assert!(c.try_borrow().is_ok());
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

#[unstable(feature = "cell_leak", issue = "69099")]
pub unsafe fn drop_leak_mut(&self) {
self.borrow.update(|x| {
assert!(x < 0, "Cell not borrowed mutably");
x + 1
});
}

/// Immutably borrows the wrapped value, returning an error if the value is
/// currently mutably borrowed.
///
Expand Down