-
Notifications
You must be signed in to change notification settings - Fork 20
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
Skip returning closure from from into_inner() #19
Conversation
src/lib.rs
Outdated
// Drop impl panicks. | ||
let value = ptr::read(&*defused.value); | ||
// drop the closure (and any captured variables consumed by it) | ||
ptr::drop_in_place(&mut *defused.dropfn); |
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.
Can't we just invert the order of the drop and then we don't need the extra manually drop?
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.
Hm ok maybe I'd instead read both vars like before and let the closure drop. It doesn't need to be more complex I think
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.
While only changing the return line would work, I don't like how correctness would then rely on the exact placement of the implicit drop. It would also need comments to explain that the closure must be read and assigned to a local variable. The new code makes that drop explicit.
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.
The diff would be like this:
#[inline]
- pub fn into_inner(guard: Self) -> (T, F) {
+ pub fn into_inner(guard: Self) -> T {
// Cannot pattern match out of Drop-implementing types, so
// ptr::read the types to return and forget the source.
unsafe {
let value = ptr::read(&*guard.value);
let dropfn = ptr::read(&*guard.dropfn);
mem::forget(guard);
- (value, dropfn)
+ value
}
}
}
it looks like the simplest solution to me.
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.
Yes, but then it's tempting to later remove let _dropfn = ptr::read(&*guard.dropfn);
or change it to let _ = ptr::read(&*guard.dropfn);
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.
Doesn't warrant complicating the function to that degree. Implicit drops are deterministic and happen in every Rust scope, so we know them well. You can insert drop(dropfn) if it needs to be emphasized
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.
OK
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 apparently forgot to actually add the changes when I amended yesterday 😄
Pushed now.
b512174
to
26d472b
Compare
26d472b
to
93e5f26
Compare
Thanks! |
Defuse is a really good word to describe its purpose! I had considered unguard or dismiss for a function that only returns the value, but decided into_inner was the least confusing.
Currently
into_inner()
triggers the wrong_self_convention clippy lint, but that gotta be a false positive and not worth disabling (clippy::
tool attributes requires a much more recent Rust version).