Implement replace_with, a function similar to the one provided by the take_mut crate.#36186
Implement replace_with, a function similar to the one provided by the take_mut crate.#36186ticki wants to merge 4 commits intorust-lang:masterfrom
replace_with, a function similar to the one provided by the take_mut crate.#36186Conversation
`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) |
src/libcore/mem.rs
Outdated
| 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.
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.
Agreed. Unfortunately, there is no possibility to do so in libcore.
There was a problem hiding this comment.
Aha. Well, perhaps libstd could do so instead of providing a straight re-export.
src/libcore/mem.rs
Outdated
| 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. |
| 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.
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.
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.
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_withinvokes 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.