This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
cargo build # Build all crates
cargo test # Run all tests (unit + integration + compile-fail)
cargo test --test integration # Run only integration tests
cargo test --test integration TESTNAME # Run a single integration test
cargo test --test compile_fail # Run compile-fail tests (trybuild)
cargo bench --bench region # Run benchmarks (criterion)
MIRIFLAGS="-Zmiri-permissive-provenance" cargo +nightly miri test -p nearest --lib --test integration # Run tests under Miri
cargo xtask fmt # Format code
cargo xtask fmt --check # Check formatting
cargo xtask lint # Clippy + format check (-D warnings)
cargo xtask lint --fix # Auto-fix clippy + formatting
cargo doc --no-deps --all --all-features # Build documentation (-D warnings in CI)
cargo deny check # License/advisory/ban auditingRequires nightly Rust (nightly-2026-02-10 pinned in rust-toolchain) for #![feature(offset_of_enum)].
nearest is a self-relative pointer library for region-based allocation. The entire data graph lives in a single contiguous byte buffer; all internal pointers are stored as i32 offsets relative to their own address, making Clone a plain memcpy with no pointer fixup.
nearest/— Main library:Near<T>,NearList<T>,Region<T>,Session, traits (Flat,Emit,Patch)nearest-derive/— Proc-macro crate:#[derive(Flat)]and#[derive(Emit)]xtask/— Dev automation (fmt, lint)
| Type | File | Role |
|---|---|---|
Flat (unsafe trait) |
flat.rs |
Marker for types storable in a region — no Drop, no heap pointers, correct deep_copy |
Emit<T> (unsafe trait) |
emit.rs |
Builder pattern for constructing values in regions; derive generates T::make(...) factories |
Patch (trait) |
patch.rs |
Unifies Emitter (construction) and Region (mutation) for position-based writes |
Near<T> |
near.rs |
Self-relative pointer: 4-byte NonZero<i32> offset, Deref<Target=T> |
NearList<T> |
list.rs |
Segmented linked list: i32 head offset + u32 length, iteration + indexing |
Region<T> |
region.rs |
Owning contiguous byte buffer with root T at byte 0 |
Session |
session.rs |
Ghost-cell branded mutable session API (Ref<'id, T>, Cursor, Brand) |
AlignedBuf<T> |
buf.rs |
Low-level growable byte buffer with alignment guarantees |
Emitter<T> |
emitter.rs |
Internal builder that writes values into an AlignedBuf during construction |
- Construction:
Region::new(T::make(...))→ derive-generatedEmitwrites values and patches self-relative offsets - Reading:
Region<T>: Deref<Target=T>,Near<T>: Deref<Target=T>(computesself_addr + offset) - Mutation:
region.session(|s| { ... })opens a branded session; mutations are append-only (old data becomes dead bytes) - Compaction:
region.trim()deep-copies only reachable data into a fresh buffer
flat.rs— GeneratesFlatimpls: enum repr validation,deep_copybody for structs/enumsemit.rs— GeneratesEmitimpls:make()factories,__Builderstructs,NearListcodegenemit_proxy.rs—Emitfor proxy enums dispatching to inner buildersutil.rs—FieldKindclassification (Primitive/Near/NearList/OptionNear/Other)
- Provenance: Uses
expose_provenance/with_exposed_provenancefor self-relative pointer resolution - Branding: Ghost-cell pattern with
for<'id>lifetime preventsRefescape from session and cross-session use (enforced at compile time) - No-Drop: Derive macro emits
const { assert!(!needs_drop::<T>()) }to reject types withDrop - Alignment: Buffer aligned to
max(align_of::<Root>(), 8) pub(crate)never relaxes safety invariants: If a function has unsafe preconditions, it must beunsafe fnregardless of visibility.pub(crate)restricts who can call it, not whether callers must uphold safety invariants.
Releases are automated with release-plz. On every push to main, the GitHub Action opens (or updates) a release PR that bumps versions and updates CHANGELOG.md. Merging that PR publishes to crates.io and creates a GitHub release with a v<version> tag.
- Config:
release-plz.toml - Workflow:
.github/workflows/release-plz.yml - Only
nearestandnearest-deriveare published;xtaskis excluded nearestowns the changelog and git tags (v{{ version }});nearest-deriveis bumped in lockstep without its own changelog or tags
Use Conventional Commits — release-plz uses them to determine version bumps and generate changelog entries.
<type>[optional scope]: <description>
[optional body]
| Type | Bump | Example |
|---|---|---|
fix: |
patch | fix: handle zero-length regions in trim |
feat: |
minor | feat: add Region::into_vec conversion |
feat!: / BREAKING CHANGE: |
major | feat!: rename Emitter to Builder |
docs:, ci:, chore:, refactor:, test: |
none | ci: pin release-plz action version |
- 2-space indentation, max 100 columns
- Imports: reordered, grouped by
StdExternalCrate, granularityCrate - Clippy:
pedantic+nurseryat warn;dbg_macro,allow_attributes,missing_safety_doc,undocumented_unsafe_blocksdenied - Dual-licensed: Apache-2.0 OR MIT