- Home
- Skills
- Zhanghandong
- Rust Skills
- M12 Lifecycle
m12-lifecycle_skill
- Shell
565
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill zhanghandong/rust-skills --skill m12-lifecycle- SKILL.md4.3 KB
Overview
This skill helps design resource lifecycles for Rust systems, focusing on when to create, use, and clean up resources. It distills patterns like RAII, lazy initialization, pools, and scope guards into practical guidance for safe, efficient resource management. Use it to choose the right ownership, scope, and error-handling strategy before implementation.
How this skill works
The skill inspects resource cost, intended scope, and error semantics to recommend a lifecycle pattern and concrete primitives (Drop, OnceLock, LazyLock, pools, guards). It maps design choices down to implementation steps and up to domain constraints so you can align lifecycle design with latency, concurrency, and infrastructure limits. It also provides templates and common fixes for leaks, double frees, and pool exhaustion.
When to use it
- Deciding where and when a resource should be created and who cleans it up.
- Designing connection pools, lazy singletons, or expensive cached resources.
- Implementing transaction or request-scoped boundaries that need deterministic cleanup.
- Choosing between automatic cleanup (RAII) and explicit close semantics.
- Handling cleanup on error or when cleanup itself can fail.
Best practices
- Prefer RAII (Drop) for guaranteed cleanup on scope exit; wrap resources in small guard structs.
- Use OnceLock/LazyLock for thread-safe lazy initialization instead of global mutable state.
- Pool expensive resources and ensure returned items are dropped/returned consistently to avoid exhaustion.
- Design clear ownership: who calls close vs who relies on Drop; avoid ambiguous manual cleanup.
- Model cleanup failures explicitly (return Result from close) when the cleanup can meaningfully fail.
Example use cases
- Temp file or socket wrapper that deletes the file in Drop (RAII guard).
- Application-wide config loaded once via OnceLock::get_or_init (lazy singleton).
- Database connection pool using r2d2/deadpool with Arc sharing and Drop-aware checkout guards.
- Transaction boundary implemented as a scope-guard struct that commits or rolls back in Drop.
- Request-scoped session objects passed through handlers and dropped at request end.
FAQ
Use Drop for mandatory cleanup that must always run on scope exit. Provide explicit close when cleanup can fail or when users should control timing; you can combine both (Drop calls close and ignores non-critical errors).
How do I avoid pool exhaustion?
Ensure checkout objects return to the pool in Drop or via explicit return. Use timeouts, capacity limits, and monitoring. Design fallbacks for when the pool is exhausted (queue, scale, or fail fast).