tl;dr async-dropper
is a crate with multiple ad-hoc definitions of AsyncDrop
-like functionality
Hey all,
After a bunch of discussion and help (thanks @alice !), I've managed to get to a working version of a crate for AsyncDrop
ing.
The crate works with both async-std
and tokio
, and works in two ways:
async_dropper::simple
This approach uses a wrapper struct (AsyncDropper
) in the way laid out by @paholg on StackOverflow.
The code in async_dropper::simple
uses the exact same mechanism, and extends support to both tokio
and asycn-std
async_dropper::derive
This approach works differently -- it introduces a derive
macro called AsyncDrop
which works for T: Default + PartialEq + Eq
.
The reason for those requirements is that it tests whether it should perform the asynchronous dropping by checking whether your object is the default
There are a whole lot of caveats/related notes:
- Your object should have cheap
Default
implementations (this means you may have to addOption
s, etc) - Waste is introduced in the form of one
static Mutex<T>
for every T you deriveAsyncDrop
on. - Waste is introduced -- each
drop()
will actually perform 3 drops:- your async functionality (implementation of
async_drop(&mut self)
- a drop on a
T::default()
(a value that was swapped withself
to enablemove
ing it to an async task) - a drop on a
T::default()
which is the&mut self
we originally wanted todrop
, afterreset(&mut self)
has been called.
- your async functionality (implementation of
This is obviously quite the hacky solution, so consider carefully if you want to use it, or if it's actually preferable to using the "simple" approach (wrapper struct).
The crate needs more tests, but for now the examples are the tests (mostly that they even work/complete).