async-drop
/async-drop
Opisuje wzorzec AsyncDrop w Rust do asynchronicznego sprzątania zasobów: AsyncDropGuard i implementacja traitu AsyncDrop.
Ocena i bezpieczeństwo
- Pliki w paczce
- 4
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 29.09.2026, bez odwołań do wycofanych modeli i API.
LGPL, otwarta.
Co robi i kiedy
Opisuje wzorzec AsyncDrop w Rust do asynchronicznego sprzątania zasobów: AsyncDropGuard i implementacja traitu AsyncDrop.
Oryginalny opis (EN)
Guide to the AsyncDrop pattern for async cleanup in Rust. Use when working with AsyncDropGuard, implementing AsyncDrop trait, or handling async resource cleanup.
Jak działa
- Core Concept
- Quick Reference
- The AsyncDrop Trait
- Essential Rules
- The withasyncdrop! Macro
- Additional References
- Location
Podgląd SKILL.md
---
name: async-drop
description: Guide to the AsyncDrop pattern for async cleanup in Rust. Use when working with AsyncDropGuard, implementing AsyncDrop trait, or handling async resource cleanup.
---
# AsyncDrop Pattern Guide
The AsyncDrop pattern enables async cleanup for types that hold resources requiring asynchronous teardown (network connections, file handles, background tasks, etc.).
## Core Concept
Rust's `Drop` trait is synchronous, but sometimes cleanup needs to be async. The AsyncDrop pattern solves this by:
1. Wrapping values in `AsyncDropGuard<T>`
2. Requiring explicit `async_drop().await` calls
3. Panicking if cleanup is forgotten
## Quick Reference
```rust
// Creating (no `mut` needed unless you mutate the value)
let guard = AsyncDropGuard::new(my_value);
// Using (transparent via Deref)
guard.do_something();
// Cleanup (REQUIRED before dropping). Consumes the guard:
// using `guard` after this line is a compile error.
guard.async_drop().await?;
```
## The AsyncDrop Trait
```rust
pub trait AsyncDrop {
type Error: Debug;
fn async_drop_impl(self) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
```
Implement it as a plain `async fn async_drop_impl(self)` without `#[async_trait]`.
`self` is taken by value, so destructure it to move `AsyncDropGuard` members out
and drop them. List every field (`name: _` for the ones that don't need dropping)
instead of using `..`: that way adding a field later is a compile error here, which
forces whoever adds an `AsyncDropGuard` field to decide how to drop it.
```rust
impl AsyncDrop for MyType {
type Error = anyhow::Error;
async fn async_drop_impl(self) -> Result<(), Self::Error> {
let Self { connection, cache, config: _ } = self;
cache.async_drop().await?;
connection.async_drop().await?;
Ok(())
}
}
```
## Essential Rules
| Rule | Description |
|------|-------------|
| **Always call async_drop()** | Every `AsyncDropGuard` must have `async_drop()` called |
| **async_drop() consumes the guard** | Use-after-drop and double-drop are compile errors |
| **Factory methods return guards** | `fn new() -> AsyncDropGuard<Self>`, never plain `Self` |
| **Types with guard members impl AsyncDrop** | `async fn async_drop_impl(self)`: destructure `self`, drop members |
| **No `#[async_trait]` on AsyncDrop impls** | The trait uses native `async fn` in traits |
| **Use the macro when possible** | `with_async_drop!` handles cleanup automatically |
| **Panics are exceptions** | It's OK to skip async_drop on panic paths |
## The `with_async_drop!` Macro
Automatically calls `async_drop()` on scope exit:
```rust
let resource = get_resource().await?;
with_async_drop!(resource, {
// Use resource here
resource.do_work().await?;
Ok(result)
})
```
Several independent guards can be listed; they are dropped concurrently afterward:
```rust
with_async_drop!(source, dest, {
move_entry(&source, &dest).await
})
```
To drop several independent guards without running a block, use `async_drop_all((a, b, c)).await?`.
## Additional References
- [patterns.md](patterns.md) - Implementation patterns and examples
- [gotchas.md](gotchas.md) - Common mistakes and how to avoid them
- [helpers.md](helpers.md) - Helper types (AsyncDropArc, AsyncDropHashMap, etc.)
## Location
Implementation: `crates/utils/src/async_drop/`
Pliki w paczce (4)
- SKILL.md 3 KB
- gotchas.md 9 KB
- helpers.md 7 KB
- patterns.md 11 KB
Wersje
W bazie nie ma innych skilli o nazwie async-drop.
Migawka bazy z 13.11.2026 · wersja paczki deea718c93e3 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100rust-async-patterns
Opisuje programowanie asynchroniczne w Rust z Tokio: async traits, obsługę błędów i wzorce współbieżne, przy budowie aplikacji asynchronicznych i debugowaniu kodu async. - 100rust-async-patterns
Prowadzi przez asynchroniczny Rust z Tokio: async traits, obsługa błędów i wzorce współbieżności w systemach równoległych oraz debugowanie kodu async. - 95rust-async-patterns
Pomaga pisać asynchroniczny kod w Rust z tokio, async/await i futures. - 100rs-lifecycle
Projektuje cykl życia zasobów w Rust: RAII, Drop, pule połączeń, leniwą inicjalizację, transakcje, sesje i wzorzec strażnika. - 90rust-systems
Buduje i rozwija usługi, CLI i komponenty systemowe w Rust z jasnymi kontraktami błędów, async i zasobów, dbając o MSRV. - 90rust-architecture
Porządkuje backend w Rust: układ workspace, moduły, obsługę błędów, runtime async i projekt sterowany typami, z flagami funkcji.