| Crates.io | async-dropx |
| lib.rs | async-dropx |
| version | 0.1.7 |
| created_at | 2025-12-03 18:39:04.469685+00 |
| updated_at | 2025-12-19 18:48:40.307033+00 |
| description | A practical crate for async destructors in Rust |
| homepage | |
| repository | https://github.com/bensantora/async-dropx |
| max_upload_size | |
| id | 1964924 |
| size | 36,395 |
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.
A practical, safe, and easy-to-use crate for "async destructors" in Rust.
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.
AsyncDrop and wrap your type.tokio and async-std (via feature flags).Add to Cargo.toml:
[dependencies]
async-dropx = { version = "0.1.0", features = ["tokio"] } # or "async-std"
Implement AsyncDrop:
use async_dropx::{AsyncDrop, AsyncDropx};
use std::pin::Pin;
use std::future::Future;
struct DatabaseConnection;
impl AsyncDrop for DatabaseConnection {
type Dropper = Pin<Box<dyn Future<Output = ()> + Send>>;
fn async_drop(self) -> Self::Dropper {
Box::pin(async move {
println!("Closing connection asynchronously...");
// await something here
})
}
}
#[tokio::main]
async fn main() {
let conn = AsyncDropx::new(DatabaseConnection);
// Use `conn` transparently (Deref coercion)
} // <--- `conn` is dropped here, and the cleanup task is spawned!
T in AsyncDropx<T>.AsyncDropx implements Deref so you can use it just like T.AsyncDropx goes out of scope, its synchronous Drop implementation runs.T and calls async_drop(T).tokio.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.