Project retired
This crate is no longer actively maintained. The repository has been archived and no additional development or support will occur.
I consent to the transfer of this crate to the first person who asks help@crates.io for it.
Thank you for any past interest and usage.
Async Dropx
A practical, safe, and easy-to-use crate for "async destructors" in Rust.
Why this crate?
Rust's Drop trait is synchronous. If you need to perform async cleanup (like closing a network connection, flushing a buffer, or sending a goodbye message) when an object goes out of scope, you're out of luck with standard Rust.
async-dropx solves this by providing a wrapper AsyncDropx<T> that detects when your object is dropped and automatically spawns a background task on your async runtime to handle the cleanup.
Features
- 🚀 Simple API: Just implement
AsyncDropand wrap your type. - ⚡ Runtime Agnostic: Works with
tokioandasync-std(via feature flags). - 🛡️ Panic Safe: Cleanup runs even if your thread panics.
- 🔧 Zero Overhead: The wrapper is a transparent newtype around your object.
Usage
Add to Cargo.toml:
[]
= { = "0.1.0", = ["tokio"] } # or "async-std"
Implement AsyncDrop:
use ;
use Pin;
use Future;
;
async // <--- `conn` is dropped here, and the cleanup task is spawned!
How it works
- You wrap your type
TinAsyncDropx<T>. AsyncDropximplementsDerefso you can use it just likeT.- When
AsyncDropxgoes out of scope, its synchronousDropimplementation runs. - It takes ownership of
Tand callsasync_drop(T). - It spawns the resulting Future on the currently active runtime.
Supported Runtimes
- Tokio: Enable feature
tokio. - Async-std: Enable feature
async-std.
If no runtime is detected or enabled, the drop will fail silently (with an error log) to avoid crashing your program, but the cleanup will not run.
Author
- Ben Santora