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
DropPtr
type (the name should be bike-shedded), which is the pointer-equivalent of a compound type that does not have aDrop
property, and change thedrop
hook to take aself
argument of this type. Since partial moves are allowed from a compound-type that does not implementDrop
, they should also be allowed fromDropPtr
. Dropping aDropPtr
behaves 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 thedrop
hook, but might take extra work to do so… and maybe recursion in the drop hook can be desirable, sometimes?) - Change
drop
to take an&move self
pointer, or even to takeself
by value. Require thatself
be destructed in the body of thedrop
hook 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
DropPtr
be calledDropFields
, orNoDropHookMovePtr
, or what? I am having a hard time identifying the “best” name for the concept. - The current
Drop
trait could be renamed toDestroy
, and similar for thedrop
method in the trait. That frees upDrop
to 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 withCopy
andMove
, 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 thanDrop
does.
Thoughts?