Email or username:

Password:

Forgot your password?
Top-level
iliazeus

@lina

> It *is* possible to forbid Drop in Rust for a type and require explicit destruction

Do you mind giving an example of how it can be done? I've been looking for a way to do this some time ago, but haven't really found anything.

5 comments
Josh Simmons

@iliazeus @lina personally, I approach this by having an explicit destroy function, which consumes the value, and then jamming an assert into the drop path which checks whether you're actually inside the explicit destroy function. You still end up with the unfortunate stuff around std::mem::forget, but in those cases I make sure it's merely a leak rather than a safety violation.

iliazeus

@dotstdy the problem with that approach, if I understand it correctly, is that it isn't compile-time. From Lina's post, I somehow thought that there's a compile-time way to do that.

@lina

Benji Smith

@iliazeus @lina Does `std::mem::ManuallyDrop` do this? Or do you mean forbid it at the type level?
doc.rust-lang.org/std/mem/stru
Edit: nvm, just saw your follow-up to that StackOverflow link

Go Up