-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement replace_with
, a function similar to the one provided by the take_mut
crate.
#36186
Conversation
`take_mut` crate. `replace_with` invokes a closure that will temporarily move out of reference and later place back the value.
Reopen later. |
(rust_highfive has picked a reviewer for you, use r? to override) |
fn drop(&mut self) { | ||
// To avoid unwinding, we abort the program, which ensures that the destructor of the | ||
// invalidated value isn't runned. | ||
unsafe { intrinsics::abort(); } |
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.
It would be nice to print a message here, ideally even a backtrace (if RUST_BACKTRACE is set).
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.
Agreed. Unfortunately, there is no possibility to do so in libcore.
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.
Aha. Well, perhaps libstd could do so instead of providing a straight re-export.
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.
Yeah
impl Drop for ExitGuard { | ||
fn drop(&mut self) { | ||
// To avoid unwinding, we abort the program, which ensures that the destructor of the | ||
// invalidated value isn't runned. |
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.
s/runned/run/
pub fn replace_with<T, F>(val: &mut T, replace: F) | ||
where F: FnOnce(T) -> T { | ||
// Guard against unwinding. Note that this is critical to safety, to avoid the value behind the | ||
// reference `val` is not dropped twice during unwinding. |
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.
Instead of "to avoid the value...is not dropped", maybe " to avoid the value...being dropped" or just "to avoid dropping the value..."
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.
your example in the comment uses mem::replace instead of mem::replace_with
Everything should be fixed now. |
} | ||
|
||
// Forget the guard, to avoid panicking. | ||
mem::forget(guard); |
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.
Fails with "Use of undeclared type or module mem
". I believe this should just be forget(guard);
Why is this PR closed? |
It was the proposed implementation for an RFC which was closed:
rust-lang/rfcs#1736
…On Fri, Oct 30, 2020 at 1:16 PM TonalidadeHidrica ***@***.***> wrote:
Why is this PR closed?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#36186 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAALPH7FHIYZ33WBQUOYKATSNLYIDANCNFSM4COKBU7A>
.
|
Oh, that's why. Sorry for the noise. |
replace_with
invokes a closure that will temporarily move out ofreference and later place back the value.
This is a part of a RFC. I'm closing it so it can be reopened later.