agentleFS
po4ykapo4yka/rust-skills/skills/rust-allocators/SKILL.md

rust-allocators

Skill7 starsChanged 30 days agoBSD-3-ClauseView on GitHub

Use when choosing or implementing a custom allocator, reviewing an Allocator contract, or putting a per-collection allocator into Box or Vec. Covers allocation failure, Layout and alignment, grow and shrink, zero-sized blocks, allocator lifetime and equivalence, and raw ownership across FFI. Not for finding allocation hot spots.

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 5 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Check the exact method and the package MSRV before use.

    line 11
  2. Very specific:

    Keep a process-wide allocator out of reusable library code.

    line 23
  3. Very specific:

    Use Layout to validate size and alignment arithmetic.

    line 42
  4. Distinctive:

    Keep live nonzero allocations disjoint.

    line 45
  5. Very specific:

    Read the contract probe when implementing an allocator adapter or adding failure injection.

    line 55
  6. Distinctive:

    Use the same tests for overridden resize methods.

    line 58
  7. Very specific:

    Use a fallible collection operation such as Vec::try_reserve for input-driven growth.

    line 63
  8. Distinctive:

    Read raw ownership and FFI when a buffer leaves Rust, a callback frees it, or a foreign owner retains it across calls.

    line 76
  9. Very specific:

    Run cargo test --locked for the allocator and its collection clients.

    line 83
  10. Very specific:

    Run those tests with cargo +<compatible-nightly> miri test --locked when the nightly supports the APIs.

    line 86
  11. Distinctive:

    Run the target and FFI tests for every shipped boundary.

    line 89

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Allocators

Select the allocation boundary before changing the allocator. Rust 1.100 stabilizes Allocator and allocator-aware Box and Vec operations. It does not stabilize all allocation APIs or allocator-aware constructors for every collection. Check the exact method and the package MSRV before use.

Choose the smallest boundary

NeedFirst choiceEvidence before a wider change
Repeated temporary buffersReuse one Vec with clear and sufficient capacityMeasure allocation calls and retained bytes
One collection needs a separate allocation policyVec<T, A> or Box<T, A>Test the collection's growth, failure, and owner lifetime
All allocations in an application need another backendApplication-owned #[global_allocator]Measure the final executable and native dependencies
One request or phase owns a regionAn allocator backed by a stable region ownerProve that every collection drops before the region resets
Foreign code owns or releases a bufferAn explicit Rust release function and opaque ownerTest one transfer and one release through the real boundary

Keep a process-wide allocator out of reusable library code. GlobalAlloc and Allocator are different contracts; an adapter needs explicit zero-size handling. The rust-hot-path skill, when installed, owns optimization after profiling; measure allocation calls and bytes with dhat before adding allocator parameters to an API.

Review the unsafe contract

Operation or conditionRequired invariantFailure to test
allocateReturn a non-null block with at least the requested size and alignmentRefuse a request with AllocError; leave other blocks intact
Zero-sized allocationReturn a valid aligned non-null result; do not forward a null backend resultRequest size zero at a large alignment, then deallocate it
allocate_zeroedInitialize all requested bytes to zeroCheck every requested byte, not only the first
deallocateAccept a live block from an equivalent allocator and a fitting layoutReturn each block once after successful and failed resize paths
grow and grow_zeroedPreserve the old contents; meet the new layout; zero the required extension for grow_zeroedOn Err, the old pointer remains valid and unchanged
shrinkPreserve the retained prefix and meet the new layoutA refused shrink also retains the original block
Successful resizeThe old block is invalid, even when the address is unchangedUse only the returned pointer and new fitting layout
Allocator lifetimeKeep backing storage and allocator identity valid for every live blockMove the owner, end a phase, or reset a region only under a proven lifetime contract
Allocation, deallocation, and allocator DropDo not unwindKeep formatting, logging, assertions, and panicking instrumentation outside allocator methods

Use Layout to validate size and alignment arithmetic. A fitting layout has the original alignment and a size within the allocation's documented requested/returned size range. A pointer to a zero-sized block does not permit a byte access. Keep live nonzero allocations disjoint. Preserve pointer provenance; an integer address is not evidence that a new owner can access or free the allocation.

Moving an allocator must preserve its equivalences. A reference to an allocator is equivalent to the referenced allocator. Follow the allocator's documented clone/equivalence contract; equal configuration is not proof of shared ownership. An allocator with inline backing storage needs an immovable owner or another proof that moving the allocator does not move live blocks. Safe shared methods such as reset(&self) cannot invalidate memory that a collection still owns.

Read the contract probe when implementing an allocator adapter or adding failure injection. It exercises default resize methods, zeroing, alignment, zero-size requests, and a failed Vec::try_reserve_exact. Use the same tests for overridden resize methods; passing the adapter probe does not verify another allocator implementation.

Handle failure at the caller

Use a fallible collection operation such as Vec::try_reserve for input-driven growth. Box::new_in and ordinary Vec growth do not turn allocation failure into an application Result. Avoid changing allocator policy solely to make an infallible constructor appear fallible. Check method stability before suggesting a try_*_in API to a stable-toolchain consumer.

On a failed reserve, check the previous elements and length before retrying or returning an error. On success, write only initialized values into the collection. An allocator returns storage, not initialized Rust values or permission to change Vec::len before construction completes.

Preserve ownership at FFI

Read raw ownership and FFI when a buffer leaves Rust, a callback frees it, or a foreign owner retains it across calls. Preserve the allocator with into_raw_parts_with_allocator or into_raw_with_allocator and reconstruct with the matching _in operation.

Verify the implementation

  1. Run cargo test --locked for the allocator and its collection clients. Include direct allocation failure, failed and successful resize, alignment, zero size, zeroing, retained values, and balanced allocation/deallocation paths.
  2. Run those tests with cargo +<compatible-nightly> miri test --locked when the nightly supports the APIs. Record the nightly and target. If it is unavailable, retain the host tests and report the missing Miri evidence.
  3. Run the target and FFI tests for every shipped boundary. A host allocator test does not establish foreign release behavior, thread safety, or region lifetimes.
  4. Measure the workload after correctness passes. Report allocation calls, live and retained bytes, and latency. Allocation reduction alone is not a speedup.

Source: Rust 1.100 Allocator contract.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.