m12-lifecycle
220
总安装量
217
周安装量
#1240
全站排名
安装命令
npx skills add https://github.com/zhanghandong/rust-skills --skill m12-lifecycle
Agent 安装分布
opencode
179
codex
158
gemini-cli
151
claude-code
149
github-copilot
137
amp
95
Skill 文档
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:
-
What’s the resource cost?
- Cheap â create per use
- Expensive â pool or cache
- Global â lazy singleton
-
What’s the scope?
- Function-local â stack allocation
- Request-scoped â passed or extracted
- Application-wide â static or Arc
-
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: domain-* (latency requirements)
â Check: Infrastructure (connection limits)
| Question | Trace To | Ask |
|---|---|---|
| Connection pooling | domain-* | What’s acceptable latency? |
| Resource limits | domain-* | What are infra constraints? |
| Transaction scope | domain-* | What must be atomic? |
Trace Down â
To implementation (Layer 1):
"Need automatic cleanup"
â m02-resource: Implement Drop
â m01-ownership: Clear owner for cleanup
"Need lazy initialization"
â m03-mutability: OnceLock for thread-safe
â m07-concurrency: LazyLock for sync
"Need connection pool"
â m07-concurrency: Thread-safe pool
â m02-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
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
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 Skills
| When | See |
|---|---|
| Smart pointers | m02-resource |
| Thread-safe init | m07-concurrency |
| Domain scopes | m09-domain |
| Error in cleanup | m06-error-handling |