/// A small wrapper that frees resources that have to be freed /// automatically when they go out of scope. pub struct DroppableValue where F: FnMut(&mut T), { value: T, drop_fn: F, } impl DroppableValue where F: FnMut(&mut T), { pub fn new(value: T, drop_fn: F) -> Self { Self { value, drop_fn } } } impl Drop for DroppableValue where F: FnMut(&mut T), { fn drop(&mut self) { (self.drop_fn)(&mut self.value); } } impl std::ops::Deref for DroppableValue where F: FnMut(&mut T), { type Target = T; fn deref(&self) -> &T { &self.value } } impl std::ops::DerefMut for DroppableValue where F: FnMut(&mut T), { fn deref_mut(&mut self) -> &mut T { &mut self.value } }