How can I panic if I a method wasn't called before the struct was dropped? E.g. I want this code to panic:
struct Foo;
impl Foo {
/// Call this before the struct is dropped.
fn finish(self) {}
}
fn main() {
let foo = Foo;
// Panic!
}
I thought of writing something like this:
struct Foo {
finish_was_called: bool,
}
impl Foo {
fn finish(mut self) {
self.finish_was_called = true;
}
}
impl Drop for Foo {
fn drop(&mut self) {
if !self.finish_was_called {
panic!("you must call finish");
}
}
}
but it doesn't feel like the best solution. I think I've seen some examples of this being done but I can't remember where. I also thought of using #[must_use]
but I don't think that would work for this use case since other methods may be called.