Some of the discussion on my linear types RFC seems locked on the semantics of the drop hook. I am hoping the discussion can move here, since this seems to be a preferred forum for fleshing out design details. Also, any change to the Drop interface should hopefully be incorporated before 1.0, so I’d hope it would be considered soon-ish.
Basically, I’d like to see it be possible to move data out of self during the drop hook. I believe there is nothing unsafe about moving data out of self during drop, and there are occasionally large benefits to doing so. One such benefit is that it is a key component of the linear types RFC, but there are others. Consider this proof-of-concept showing how allowing partial moves from self during drop could be used to enforce that a memory block allocated from a pool be returned to the same pool from which it was allocated. This type of pattern is greatly simplified by allowing partial moves from self during drop.
While there are (I believe) significant benefits to allowing partial moves from drop, we’d still need to find the right mechanism to make the best design possible, given the design goals of the language. There are a couple of ideas floating around.
First, the mechanisms (ignore the names, for now, I’ll talk about them below):
- (From my proposal) Define a new
DropPtrtype (the name should be bike-shedded), which is the pointer-equivalent of a compound type that does not have aDropproperty, and change thedrophook to take aselfargument of this type. Since partial moves are allowed from a compound-type that does not implementDrop, they should also be allowed fromDropPtr. Dropping aDropPtrbehaves identically to dropping a compound variable that did not implementDrop: any partially-moved fields are not dropped, while any fields that remain in the container get dropped as normal. The referent of the DropPtr can be moved, so long as no fields have been partially dropped. (TBD: does this work? It could be used to enable recursion in thedrophook, but might take extra work to do so… and maybe recursion in the drop hook can be desirable, sometimes?) - Change
dropto take an&move selfpointer, or even to takeselfby value. Require thatselfbe destructed in the body of thedrophook to prevent recursively invoking the drop hook from within the drop hook. (@Ericson2314, please correct me if I misunderstand this part of the proposal, I fear I’m unable to do it justice.)
Then there are the parts of the proposals centering around API names:
- Regarding proposal #1 above, should
DropPtrbe calledDropFields, orNoDropHookMovePtr, or what? I am having a hard time identifying the “best” name for the concept. - The current
Droptrait could be renamed toDestroy, and similar for thedropmethod in the trait. That frees upDropto mean “can be implicitly dropped” (which would be nice for linear-types support, since that would make linear types!Drop, and also has a nice relationship withCopyandMove, which are other traits where the compiler itself would treat variables of these types differently), and probably better captures the idea that these types have destructors thanDropdoes.
Thoughts?