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

Skip returning closure from from into_inner() #19

Merged
merged 1 commit into from
Dec 18, 2018

Conversation

tormol
Copy link
Contributor

@tormol tormol commented Dec 17, 2018

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

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);
Copy link
Owner

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?

Copy link
Owner

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

Copy link
Contributor Author

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.

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Contributor Author

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.

@bluss
Copy link
Owner

bluss commented Dec 18, 2018

Thanks!

@bluss bluss merged commit fffb6e6 into bluss:master Dec 18, 2018
@tormol tormol deleted the into_only_value branch December 21, 2018 19:16
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.

2 participants