-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
drop could take self by-value, and allow early destruction #4330
Comments
Now that we can move out of methods, could we just change the |
@erickt: that was actually the original idea, and the idea of doing it with a trait actually came later to avoid needing compiler changes since I hadn't considered that it would result in a destruction cycle. However, it turns out there's already |
I was thinking this would be useful as well, but I don't know how it can work. During destruction the allocator essentially 'owns' the value and by passing self to |
I do think that the finalizer should take self "by value". @brson I'm not quite sure what you think would be wrong with that. |
Nominating for milestone 2. |
We discussed at the work week why by-value finalizers are ok and you convinced me at the time, but yet again I don't understand how that works. |
Short version: what we call "by-value" really just means that ownership of the contents is transferred to the callee. So the callee is responsible for freeing the contents, basically. In most cases, the value of a "by-value" parameter is still passed by pointer, it's just that we create a value on our stack and give a pointer to the callee. They are responsible then to free the data in that stack region, we don't free its contents. This seems to match destructors perfectly, if you generalize "stack region" to "memory region". |
It isn't true though that the |
Yes, today the compiler generates the code to free the contents, but that's precisely what I propose to change. Instead, the contents would be freed (or sent, or moved) by the destructor, just as with any other by-value argument. I don't see a problem with the fact that the destructor can send or move |
oops, closed by accident. |
@nikomatsakis If the finalizer moves Here's some code, annotated with what I think you are proposing.
In both those blocks a
|
Hmm, so, I think if you move self as a whole, then it would re-run the finalizer. The only magic thing, I suppose, is that the finalizer does not run on |
We discussed this at the meeting today. Conclusion was that dtors |
On Thu, Jun 27, 2013 at 12:54:01AM -0700, Daniel Micay wrote:
There is nothing "safe" about this flag, as far as I can tell? You |
@nikomatsakis: yup, it's not actually safe - that's why I deleted the comment :) |
My current opinion as that there are too many special cases required to support by-value finalizers, not enough real use cases, and |
We decided in today's meeting (https://github.com/mozilla/rust/wiki/Meeting-weekly-2013-10-22) that drop by value requires too many special rules and semantics about it to warrant the convenience of taking self by value instead of by reference. For that reason, I'm de-milestoning the bug, removing the P-backcompat-lang label, and removing the I-completion label. While we don't plan on doing this for 1.0, there is always the possibility of doing it afterwards. It would be a little painful, but types could implement a |
I no longer think this is a great idea. It's certainly possible to implement with special cases in the language, but I don't think the additional complexity is worth it. It's easy enough to do anything you could do this way by using |
In case anyone is still interested, I found a crate https://github.com/terrarier2111/owned-drop that enables getting ownership of dropped data without using |
There's currently a
util::ignore
function that consumes a value to destroy it early.It would be nicer the if
Drop
trait'sdrop
method tookself
by value and could be called early, which would require some compiler magic to avoid an infinite destruction cycle in thedrop
method. This would also allowdrop
to move out ofself
, which is potentially useful.The text was updated successfully, but these errors were encountered: