# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Build and Test Commands
```bash
cargo build # Build the library
cargo test # Run all tests
cargo test test_name # Run a specific test
cargo clippy # Run linter
cargo fmt # Format code
cargo bench # Run benchmarks (compares Wrc vs Arc vs Rc)
```
## Architecture
This is a Rust library implementing a thread-safe weighted reference counting smart pointer (`Wrc<T>`), an alternative to `Arc<T>` that uses weight-splitting to reduce atomic operations.
### Core Components
- **`Wrc<T>`** (`src/wrc.rs`) - The main smart pointer type. Implements `Clone`, `Deref`, `Drop`, and standard traits. Each instance holds a local weight and a pointer to the shared `Inner<T>`.
- **`Weak<T>`** (`src/wrc.rs`) - Non-owning weak reference for breaking cycles. Created via `Wrc::downgrade()`, can be upgraded back to `Wrc` if the value still exists.
- **`Inner<T>`** (`src/inner.rs`) - Heap-allocated storage containing the data and two weight counters: `strong_weight` (for `Wrc` references) and `weak_weight` (for `Weak` references plus one for all strong refs).
### How Weighted Reference Counting Works
Instead of incrementing/decrementing a counter on clone/drop, weight is split:
1. New `Wrc` starts with weight `1 << 16` (65536), both locally and in `Inner`
2. On clone, local weight is halved - parent keeps half, child gets half (no atomic write to `Inner`)
3. On drop, local weight is subtracted from `Inner`'s total (one atomic operation)
4. When local weight reaches 1 and clone is needed, new weight is allocated from `Inner`
5. When `Inner`'s strong weight reaches 0, data is dropped; when weak weight reaches 0, `Inner` is deallocated
This halves atomic operations compared to traditional reference counting.