Summary
Extending std::ops::Drop trait so that user can specify whether the object should be dropped at the end of the enclosing block(the current behavior) or as early as possible.
Motivation
There are types that it’s almost always preferred to drop as early as possible, e.g., Mutex, RwLock. Those will have optimal behavior automatically.
Detailed design
Ideally I think dependent type is the best way to express this, which Rust doesn’t have yet though.
impl std::ops::Drop for Foo {
let DROP_STRATEGY : DropStrategy = DropStrategy::Early;
// let DROP_STRATEGY : DropStrategy = DropStrategy::Late; // Or this.
fn drop(&mut self) {
// ...
}
}
The dropping timing will be controlled by Foo::DROP_STRATEGY enum value.
How We Teach This
TBA
Drawbacks
Complicating Drop.
Alternatives
Using blocks {} to control drop timing.
Unresolved questions
None.