-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 undo_leak to reset RefCell borrow state #69528
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
r? @dtolnay |
I would have expected this to not take |
This is a completely safe interface that fully resets the borrows. I just noticed that the name might be suboptimal as the implementation doesn't differentiate between shared and mut borrows and keeping the name The |
Other names that are possible and come to mind:
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
I'm not really opposed to this interface but I personally suspect almost everyone will want the raw unsafe API rather than this. |
Maybe emphasize in the PR description that this allows safely reverting the effects of leaking a borrow. |
This comment has been minimized.
This comment has been minimized.
2c9ff08
to
dbd9847
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am on board with adding a safe unborrow API but we will need to pay attention during stabilization that we have been able to identify some concrete examples where using this API is the best solution.
The most important consideration when adding niche methods is how to keep them from being a distraction when people are looking for the non-niche behaviors they care about. I think unborrow
isn't great in that aspect. Instead I would expect something like:
fn undo_leak(&mut self);
// later, if needed:
unsafe fn force_undo_leak(&self);
From these names it is clear that if someone isn't planning on leaking refs then these methods are not going to be relevant to them.
This method is complementary for the feature refcell_leak added in an earlier PR. It allows reverting the effects of leaking a borrow guard by statically proving that such a guard could not longer exist. This was not added to the existing `get_mut` out of concern of impacting the complexity of the otherwise pure pointer cast and because the name `get_mut` poorly communicates the intent of resetting remaining borrows.
dbd9847
to
51b9396
Compare
The name suggestion seems very clearly motivated and convincing to me, so I changed the PR. |
The commit above my comment addresses the review (I think), I'm not sure why this is now tagged as |
@dtolnay this is awaiting your (re-)review. |
/// assert!(c.try_borrow().is_ok()); | ||
/// ``` | ||
#[unstable(feature = "cell_leak", issue = "69099")] | ||
pub fn undo_leak(&mut self) -> &mut T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the motivation for returning a reference here? The example does not even make use of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had previously seen it as a strengthened version of get_mut
. It could be fine fully separating the two methods tough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
@bors r+ |
📌 Commit 51b9396 has been approved by |
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
This method is complementary for the feature cell_leak added in an
earlier PR. It allows safely reverting the effects of leaking a borrow guard by
statically proving that such a guard could not longer exist. This was
not added to the existing
get_mut
out of concern of impacting thecomplexity of the otherwise pure pointer cast and because the name
get_mut
poorly communicates the intent of resetting remaining borrows.This is a follow-up to #68712 and uses the same tracking issue, #69099,
as these methods deal with the same mechanism and the idea came up
in a review comment.
@dtolnay who reviewed the prior PR.
cc @RalfJung