basedrop
basedrop copied to clipboard
best effort drop implementation
This adds a Drop implementation for Collector that tries to not leak memory. If there is a Handle or Allocation of the Collector if will still leak memory as waiting for that inside Drop is a bad idea.
I can't think of a use-case where you wouldn't want to run collect & try_cleanup before dropping Collector, so this protects against mistakes. I don't think performance is very important, as this only affects the thread which collects (so not a real-time thread) and also only when dropping the Collector which should happen pretty rarely. I also added can_deallocate function to decide wether to drop or not. This in result is pretty similar to the existing try_cleanup, just without the weird Result::Err reassign dance.
It also makes embedding Collector into other data structures much more ergonomic as you don't need ManuallyDrop to ensure that no memory gets leaked. Example before:
struct Wrapper(ManuallyDrop<Collector>); impl Drop for Wrapper { fn drop(&mut self) { let mut collector = self.0.take(); collector.collect(); assert!(collector.try_cleanup().is_ok());
Example now:
I also added a call to Collector::collect() inside of try_cleanup, because i think that that is part of trying to clean up. That isn't the core of this PR, so if you don't like that i would be happy to remove it.