Loading...
Loading...
Guide to the AsyncDrop pattern for async cleanup in Rust. Use when working with AsyncDropGuard, implementing AsyncDrop trait, or handling async resource cleanup.
npx skill4agent add cryfs/cryfs async-dropDropAsyncDropGuard<T>async_drop().await// Creating
let mut guard = AsyncDropGuard::new(my_value);
// Using (transparent via Deref)
guard.do_something();
// Cleanup (REQUIRED before dropping)
guard.async_drop().await?;#[async_trait]
pub trait AsyncDrop {
type Error: Debug;
async fn async_drop_impl(&mut self) -> Result<(), Self::Error>;
}| Rule | Description |
|---|---|
| Always call async_drop() | Every |
| Factory methods return guards | |
| Types with guard members impl AsyncDrop | Delegate to member async_drops |
| Use the macro when possible | |
| Panics are exceptions | It's OK to skip async_drop on panic paths |
with_async_drop_2!async_drop()let resource = get_resource().await?;
with_async_drop_2!(resource, {
// Use resource here
resource.do_work().await?;
Ok(result)
})crates/utils/src/async_drop/