CRITICAL: Use for smart pointers and resource management. Triggers: Box, Rc, Arc, Weak, RefCell, Cell, smart pointer, heap allocation, reference counting, RAII, Drop, should I use Box or Rc, when to use Arc vs Rc, ๆบ่ฝๆ้, ๅผ็จ่ฎกๆฐ, ๅ ๅ้
Installation
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
CRITICAL: Use for smart pointers and resource management. Triggers: Box, Rc, Arc, Weak, RefCell, Cell, smart pointer, heap allocation, reference counting, RAII, Drop, should I use Box or Rc, when to use Arc vs Rc, ๆบ่ฝๆ้, ๅผ็จ่ฎกๆฐ, ๅ ๅ้
user-invocable
false
Resource Management
Layer 1: Language Mechanics
Core Question
What ownership pattern does this resource need?
Before choosing a smart pointer, understand:
Is ownership single or shared?
Is access single-threaded or multi-threaded?
Are there potential cycles?
Error โ Design Question
Error
Don't Just Say
Ask Instead
"Need heap allocation"
"Use Box"
Why can't this be on stack?
Rc memory leak
"Use Weak"
Is the cycle necessary in design?
RefCell panic
"Use try_borrow"
Is runtime check the right approach?
Arc overhead complaint
"Accept it"
Is multi-thread access actually needed?
Thinking Prompt
Before choosing a smart pointer:
What's the ownership model?
Single owner โ Box or owned value
Shared ownership โ Rc/Arc
Weak reference โ Weak
What's the thread context?
Single-thread โ Rc, Cell, RefCell
Multi-thread โ Arc, Mutex, RwLock
Are there cycles?
Yes โ One direction must be Weak
No โ Regular Rc/Arc is fine
Trace Up โ
When pointer choice is unclear, trace to design:
"Should I use Arc or Rc?"
โ Ask: Is this data shared across threads?
โ Check: m07-concurrency (thread model)
โ Check: domain-* (performance constraints)
Need heap allocation?
โโ Yes โ Single owner?
โ โโ Yes โ Box<T>
โ โโ No โ Multi-thread?
โ โโ Yes โ Arc<T>
โ โโ No โ Rc<T>
โโ No โ Stack allocation (default)
Have reference cycles?
โโ Yes โ Use Weak for one direction
โโ No โ Regular Rc/Arc
Need interior mutability?
โโ Yes โ Thread-safe needed?
โ โโ Yes โ Mutex<T> or RwLock<T>
โ โโ No โ T: Copy? โ Cell<T> : RefCell<T>
โโ No โ Use &mut T