Just an idea:
trait TryDrop {
type Error;
fn try_drop(&mut self) -> Result<(), Self::Error>;
}
and a T
that implements TryDrop
can only be dropped in a scope that returns Result<T, U> where U: From<<T as TryDrop>::Error>
so code like this:
impl TryDrop for File {
/* -snip- */
}
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("foo")?;
// do something with file
Ok(())
}
is converted to code like this
/* -snip- */
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("foo")?;
// do something with file
file.try_drop()?;
Ok(())
}
similarly how Drop
works (sort of)