Async Drop
Inspired by async-dropper
Adjustments
- Removed
async_traitcrate dependency - Types don't have to implement
Default - Dropper's drop will wait until
async_dropcompletes - Only compatible with the
tokioruntime (for now)
Usage
{
Box::pin(async {
tokio::time::sleep(Duration::from_secs(3)).await;
println!("dropped");
Ok(())
})
}
}
#[tokio::main]
async fn main() {
{
let _thing = Dropper::new(Thing);
println!("dropping...");
} // `_thing` is dropped here, but before that happens `async_drop()` will run to completion
}
">struct Thing;
impl AsyncDrop for Thing {
fn async_drop(&mut self) -> AsyncDropFuture<'_> {
Box::pin(async {
tokio::time::sleep(Duration::from_secs(3)).await;
println!("dropped");
Ok(())
})
}
}
#[tokio::main]
async fn main() {
{
let _thing = Dropper::new(Thing);
println!("dropping...");
} // `_thing` is dropped here, but before that happens `async_drop()` will run to completion
}
impl AsyncDrop for Thing {
fn async_drop(&mut self) -> AsyncDropFuture<'_> {
Box::pin(async {
tokio::time::sleep(Duration::from_secs(3)).await;
println!("dropped");
Ok(())
})
}
}
#[tokio::main]
async fn main() {
{
let _thing = Dropper::new(Thing);
println!("dropping...");
} // `_thing` is dropped here, but before that happens `async_drop()` will run to completion
}
Examples
See test and example directories