โ† Back to list
rustfs

m02-resource

by rustfs

๐Ÿš€2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system supporting migration and coexistence with other S3-compatible platforms such as MinIO and Ceph.

โญ 20,125๐Ÿด 866๐Ÿ“… Jan 23, 2026

SKILL.md


name: m02-resource description: "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, ๆ™บ่ƒฝๆŒ‡้’ˆ, ๅผ•็”จ่ฎกๆ•ฐ, ๅ †ๅˆ†้…"

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

ErrorDon't Just SayAsk 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:

  1. What's the ownership model?

    • Single owner โ†’ Box or owned value
    • Shared ownership โ†’ Rc/Arc
    • Weak reference โ†’ Weak
  2. What's the thread context?

    • Single-thread โ†’ Rc, Cell, RefCell
    • Multi-thread โ†’ Arc, Mutex, RwLock
  3. 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)
SituationTrace ToQuestion
Rc vs Arc confusionm07-concurrencyWhat's the concurrency model?
RefCell panicsm03-mutabilityIs interior mutability right here?
Memory leaksm12-lifecycleWhere should cleanup happen?

Trace Down โ†“

From design to implementation:

"Need single-owner heap data"
    โ†“ Use: Box<T>

"Need shared immutable data (single-thread)"
    โ†“ Use: Rc<T>

"Need shared immutable data (multi-thread)"
    โ†“ Use: Arc<T>

"Need to break reference cycle"
    โ†“ Use: Weak<T>

"Need shared mutable data"
    โ†“ Single-thread: Rc<RefCell<T>>
    โ†“ Multi-thread: Arc<Mutex<T>> or Arc<RwLock<T>>

Quick Reference

TypeOwnershipThread-SafeUse When
Box<T>SingleYesHeap allocation, recursive types
Rc<T>SharedNoSingle-thread shared ownership
Arc<T>SharedYesMulti-thread shared ownership
Weak<T>Weak refSame as Rc/ArcBreak reference cycles
Cell<T>SingleNoInterior mutability (Copy types)
RefCell<T>SingleNoInterior mutability (runtime check)

Decision Flowchart

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

Common Errors

ProblemCauseFix
Rc cycle leakMutual strong refsUse Weak for one direction
RefCell panicBorrow conflict at runtimeUse try_borrow or restructure
Arc overheadAtomic ops in hot pathConsider Rc if single-threaded
Box unnecessaryData fits on stackRemove Box

Anti-Patterns

Anti-PatternWhy BadBetter
Arc everywhereUnnecessary atomic overheadUse Rc for single-thread
RefCell everywhereRuntime panicsDesign clear ownership
Box for small typesUnnecessary allocationStack allocation
Ignore Weak for cyclesMemory leaksDesign parent-child with Weak

WhenSee
Ownership errorsm01-ownership
Interior mutability detailsm03-mutability
Multi-thread contextm07-concurrency
Resource lifecyclem12-lifecycle

Score

Total Score

90/100

Based on repository quality metrics

โœ“SKILL.md

SKILL.mdใƒ•ใ‚กใ‚คใƒซใŒๅซใพใ‚Œใฆใ„ใ‚‹

+20
โœ“LICENSE

ใƒฉใ‚คใ‚ปใƒณใ‚นใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+10
โœ“่ชฌๆ˜Žๆ–‡

100ๆ–‡ๅญ—ไปฅไธŠใฎ่ชฌๆ˜ŽใŒใ‚ใ‚‹

+10
โœ“ไบบๆฐ—

GitHub Stars 1000ไปฅไธŠ

+15
โœ“ๆœ€่ฟ‘ใฎๆดปๅ‹•

1ใƒถๆœˆไปฅๅ†…ใซๆ›ดๆ–ฐ

+10
โœ“ใƒ•ใ‚ฉใƒผใ‚ฏ

10ๅ›žไปฅไธŠใƒ•ใ‚ฉใƒผใ‚ฏใ•ใ‚Œใฆใ„ใ‚‹

+5
โ—‹Issue็ฎก็†

ใ‚ชใƒผใƒ—ใƒณIssueใŒ50ๆœชๆบ€

0/5
โœ“่จ€่ชž

ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+5
โœ“ใ‚ฟใ‚ฐ

1ใคไปฅไธŠใฎใ‚ฟใ‚ฐใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+5

Reviews

๐Ÿ’ฌ

Reviews coming soon