rs-lifecycle
/rs-lifecycle
Projektuje cykl życia zasobów w Rust: RAII, Drop, pule połączeń, leniwą inicjalizację, transakcje, sesje i wzorzec strażnika.
- Autor
- bassem-elsodany ↗
- Repozytorium
- rs-skills ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 11.11.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 2
- Skrypty
- 0
- Komentarze HTML
- 2
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 11.11.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
- SKILL.md zawiera komentarze HTML (2). Nie widać ich w podglądzie Markdown, a model je czyta.
Co robi i kiedy
Projektuje cykl życia zasobów w Rust: RAII, Drop, pule połączeń, leniwą inicjalizację, transakcje, sesje i wzorzec strażnika.
Oryginalny opis (EN)
Use when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup patterns, cleanup, scope, OnceCell, Lazy, once_cell, OnceLock, transaction, session management, when is Drop called, cleanup on error, guard pattern, scope guard
Jak działa
- Core Question
- Lifecycle Pattern → Implementation
- Thinking Prompt
- Trace Up ↑
- Trace Down ↓
- Quick Reference
- Lifecycle Events
- Pattern Templates
- Common Errors
- Anti-Patterns
Podgląd SKILL.md
---
name: rs-lifecycle
description: "Use when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup patterns, cleanup, scope, OnceCell, Lazy, once_cell, OnceLock, transaction, session management, when is Drop called, cleanup on error, guard pattern, scope guard"
user-invocable: false
---
# Resource Lifecycle
> **Layer 2: Design Choices**
## Core Question
**When should this resource be created, used, and cleaned up?**
Before implementing lifecycle:
- What's the resource's scope?
- Who owns the cleanup responsibility?
- What happens on error?
---
## Lifecycle Pattern → Implementation
| Pattern | When | Implementation |
|---------|------|----------------|
| RAII | Auto cleanup | `Drop` trait |
| Lazy init | Deferred creation | `OnceLock`, `LazyLock` |
| Pool | Reuse expensive resources | `r2d2`, `deadpool` |
| Guard | Scoped access | `MutexGuard` pattern |
| Scope | Transaction boundary | Custom struct + Drop |
---
## Thinking Prompt
Before designing lifecycle:
1. **What's the resource cost?**
- Cheap → create per use
- Expensive → pool or cache
- Global → lazy singleton
2. **What's the scope?**
- Function-local → stack allocation
- Request-scoped → passed or extracted
- Application-wide → static or Arc
3. **What about errors?**
- Cleanup must happen → Drop
- Cleanup is optional → explicit close
- Cleanup can fail → Result from close
---
## Trace Up ↑
To domain constraints (Layer 3):
```
"How should I manage database connections?"
↑ Ask: What's the connection cost?
↑ Check: rs-domain-* (latency requirements)
↑ Check: Infrastructure (connection limits)
```
| Question | Trace To | Ask |
|----------|----------|-----|
| Connection pooling | rs-domain-* | What's acceptable latency? |
| Resource limits | rs-domain-* | What are infra constraints? |
| Transaction scope | rs-domain-* | What must be atomic? |
---
## Trace Down ↓
To implementation (Layer 1):
```
"Need automatic cleanup"
↓ rs-resource: Implement Drop
↓ rs-ownership: Clear owner for cleanup
"Need lazy initialization"
↓ rs-mutability: OnceLock for thread-safe
↓ rs-concurrency: LazyLock for sync
"Need connection pool"
↓ rs-concurrency: Thread-safe pool
↓ rs-resource: Arc for sharing
```
---
## Quick Reference
| Pattern | Type | Use Case |
|---------|------|----------|
| RAII | `Drop` trait | Auto cleanup on scope exit |
| Lazy Init | `OnceLock`, `LazyLock` | Deferred initialization |
| Pool | `r2d2`, `deadpool` | Connection reuse |
| Guard | `MutexGuard` | Scoped lock release |
| Scope | Custom struct | Transaction boundaries |
## Lifecycle Events
| Event | Rust Mechanism |
|-------|----------------|
| Creation | `new()`, `Default` |
| Lazy Init | `OnceLock::get_or_init` |
| Usage | `&self`, `&mut self` |
| Cleanup | `Drop::drop()` |
## Pattern Templates
### RAII Guard
```rust
struct FileGuard {
path: PathBuf,
_handle: File,
}
impl Drop for FileGuard {
fn drop(&mut self) {
// Cleanup: remove temp file
let _ = std::fs::remove_file(&self.path);
}
}
```
### Lazy Singleton
```rust
use std::sync::OnceLock;
static CONFIG: OnceLock<Config> = OnceLock::new();
fn get_config() -> &'static Config {
CONFIG.get_or_init(|| {
Config::load().expect("config required")
})
}
```
---
## Common Errors
| Error | Cause | Fix |
|-------|-------|-----|
| Resource leak | Forgot Drop | Implement Drop or RAII wrapper |
| Double free | Manual memory | Let Rust handle |
| Use after drop | Dangling reference | Check lifetimes |
| E0509 move out of Drop | Moving owned field | `Option::take()` |
| Pool exhaustion | Not returned | Ensure Drop returns |
---
## Anti-Patterns
| Anti-Pattern | Why Bad | Better |
|--------------|---------|--------|
| Manual cleanup | Easy to forget | RAII/Drop |
| `lazy_static!` | External dep | `std::sync::OnceLock` |
| Global mutable state | Thread unsafety | `OnceLock` or proper sync |
| Forget to close | Resource leak | Drop impl |
---
## Related patterns
| Problem | Pattern / idiom | Page (rs-patterns) |
|---|---|---|
| Acquire in constructor, release in `Drop`, hold while used | RAII guards | `../rs-patterns/design-patterns/behavioural/RAII.md` |
| A `finally` block | Finalisation in destructors | `../rs-patterns/idioms/dtor-finally.md` |
| Creating resources with sensible defaults | Constructor + `Default` | `../rs-patterns/idioms/ctor.md` |
Decision guide for all 52 pages: `../rs-patterns/SKILL.md`.
---
<!-- rules-index:start -->
## Rules (1)
Depth rules under `rules/` — open the one the question needs, not all of them.
**mem-**
- [`mem-drop-order`](rules/mem-drop-order.md) — Know and control drop order: struct fields drop top-to-bottom, locals in reverse
<!-- rules-index:end -->
---
## Related Skills
| When | See |
|------|-----|
| Smart pointers | rs-resource |
| Thread-safe init | rs-concurrency |
| Domain scopes | rs-domain-model |
| Error in cleanup | rs-error-handling |
| Graceful shutdown of tasks and pools | rs-async-patterns |
Pliki w paczce (2)
- SKILL.md 5 KB
- rules/mem-drop-order.md 5 KB
Wersje
W bazie nie ma innych skilli o nazwie rs-lifecycle.
Migawka bazy z 13.11.2026 · wersja paczki d606efe39055 (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
- 100async-drop
Opisuje wzorzec AsyncDrop w Rust do asynchronicznego sprzątania zasobów: AsyncDropGuard i implementacja traitu AsyncDrop. - 100connection-pool-analyzer
Analizuje i audytuje pule połączeń w testach wydajności, uruchamiając się automatycznie na frazy związane z analizatorem puli połączeń. - 90python-data-access-transaction
Statycznie przegląda dostęp do bazy i transakcje w Pythonie (SQLAlchemy, Django ORM, DB-API): zakres sesji, commit i rollback, N+1, pule połączeń, bezpieczeństwo migracji i multi-tenancy. - 100python-resource-management
Pilnuje zasobów w Pythonie: menedżery kontekstu, sprzątanie połączeń i plików oraz strumieniowe odpowiedzi z zebranym stanem. - 100memory-safety-patterns
Wdraża programowanie bezpieczne pamięciowo z RAII, własnością, inteligentnymi wskaźnikami i zarządzaniem zasobami w Rust, C++ i C, aby zapobiegać błędom pamięci. - 95effect-resource-management
Opisuje zarządzanie zasobami w Effect: Scope, addFinalizer, efekty w zakresie i automatyczne sprzątanie zasobów.