m12-lifecycle

zhanghandong/rust-skills · updated Apr 8, 2026

$npx skills add https://github.com/zhanghandong/rust-skills --skill m12-lifecycle
0 commentsdiscussion
summary

Design resource creation, cleanup, and scope using RAII, lazy initialization, and pooling patterns.

  • Covers five lifecycle patterns: RAII with Drop trait, lazy initialization via OnceLock/LazyLock, connection pooling with r2d2/deadpool, guard-based scoped access, and transaction scope boundaries
  • Includes decision framework for resource cost, scope determination, and error handling during cleanup
  • Provides pattern templates for RAII guards and lazy singletons, plus common errors and ant
skill.md

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: 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

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.729 reviews
  • Isabella Iyer· Dec 28, 2024

    m12-lifecycle fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.

  • Shikha Mishra· Dec 20, 2024

    Registry listing for m12-lifecycle matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Neel Srinivasan· Dec 12, 2024

    I recommend m12-lifecycle for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Neel Tandon· Nov 19, 2024

    We added m12-lifecycle from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Yash Thakker· Nov 11, 2024

    m12-lifecycle reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Harper Garcia· Nov 7, 2024

    Useful defaults in m12-lifecycle — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Kaira Sanchez· Nov 3, 2024

    Keeps context tight: m12-lifecycle is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Amina Reddy· Oct 22, 2024

    Registry listing for m12-lifecycle matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Neel Jackson· Oct 10, 2024

    Solid pick for teams standardizing on skills: m12-lifecycle is focused, and the summary matches what you get after install.

  • Dhruvi Jain· Oct 2, 2024

    I recommend m12-lifecycle for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

showing 1-10 of 29

1 / 3