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

Implement replace_with, a function similar to the one provided by the take_mut crate. #36186

Closed
wants to merge 4 commits into from

Conversation

ticki
Copy link
Contributor

@ticki ticki commented Sep 1, 2016

replace_with invokes a closure that will temporarily move out of
reference and later place back the value.

This is a part of a RFC. I'm closing it so it can be reopened later.

`take_mut` crate.

`replace_with` invokes a closure that will temporarily move out of
reference and later place back the value.
@ticki
Copy link
Contributor Author

ticki commented Sep 1, 2016

Reopen later.

@ticki ticki closed this Sep 1, 2016
@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@ticki
Copy link
Contributor Author

ticki commented Sep 1, 2016

rust-lang/rfcs#1736

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(); }
Copy link
Contributor

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).

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.
Copy link
Contributor

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..."

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

@ticki ticki reopened this Oct 13, 2016
@ticki ticki closed this Oct 13, 2016
@ticki
Copy link
Contributor Author

ticki commented Oct 13, 2016

Everything should be fixed now.

}

// Forget the guard, to avoid panicking.
mem::forget(guard);
Copy link
Member

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);

@TonalidadeHidrica
Copy link
Contributor

Why is this PR closed?

@durka
Copy link
Contributor

durka commented Oct 30, 2020 via email

@TonalidadeHidrica
Copy link
Contributor

Oh, that's why. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.