🗑 async-dropper: the least worst ad-hoc AsyncDrop so far?

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 AsyncDroping.

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 add Options, etc)
  • Waste is introduced in the form of one static Mutex<T> for every T you derive AsyncDrop 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 with self to enable moveing it to an async task)
    • a drop on a T::default() which is the &mut self we originally wanted to drop, after reset(&mut self) has been called.

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).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.