heap_arr 0.4.0

Allocate fixed-size arrays directly on the heap, bypassing stack allocation limits
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented8 out of 8 items with examples
  • Size
  • Source code size: 15.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 956.74 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • fstasiak/heap_arr
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • fstasiak

heap_arr

Allocate fixed-size arrays directly on the heap. For #![no_std] environments too.

The Problem

In Rust, Box::new([T; N]) allocates on the stack first, then moves to the heap. For large arrays, this can overflow the stack. Rust's optimizer should handle this in optimized builds, but not in debug.

The Solution

// Instead of this:
let arr = Box::new([0_u8; 1_000_000_000]); // Stack overflow in debug

// Use this:
let arr: Box<[u8; 1_000_000_000]> = heap_arr::new_copied(&0_u8); // Allocates directly on heap

Fill arrays via new_cloned(), new_default(), from_fn(), try_from_fn(), or grab uninitialized memory with uninit() and initialize it yourself.

Everything works in terms of Box<[T; N]> like the standard library. No dependencies outside alloc, and works in #![no_std].