4 ms·
You can kind of achieve this at runtime like so struct Foo { bool dirty, } impl Foo { fn clean_up(&mut self) { // ...
by bqmjjx0kac 2y ago
You can kind of achieve this at runtime like so
struct Foo {
bool dirty,
}
impl Foo {
fn clean_up(&mut self) {
// ...
self.dirty = false;
}
}
impl Drop for Foo {
fn drop(&mut self) {
if self.dirty {
panic!("Foo was not cleaned up before its lifetime ended");
}
}
}
fn bar(foo: &mut foo) {
foo.clean_up();
}
- surajrmal 2y agoA method which takes foo and turns it into another type is slightly better. You can then unconditionally panic in the drop impl.
- bqmjjx0kac 2y agoNeat, I didn't realize that would skip drop()!