9 releases (5 breaking)

0.6.1 Sep 11, 2020
0.6.0 Apr 29, 2020
0.5.0 Apr 17, 2020
0.4.1 Apr 17, 2020
0.1.0 Apr 11, 2020

⚠️ Issues reported

#322 in Template engine

23 downloads per month

MIT license

12KB
250 lines

The arr crate provides a single fixed-sized array data-structure that is purely heap-based.

Crates.io

Docs

Include:

[dependencies]
arr = "0.6.0"

Basic usage:

use arr::Array;

// Allocate a 16MB chunk of zeros as 16 byte sub-arrays
let huge_array: Array<[u8; 16]> = Array::zero(1 << 20);
println!("{}", huge_array.len());

The motivation for this crate comes from the limitation of rust to easily allocate heap-based arrays without blowing the stack.

This likely will blow your stack:

let huge_array: [[u8; 16]; 1 << 20] = [[0u8; 16]; 1 << 20];

lib.rs:

The arr crate is a simple fixed size array designed to allow huge array allocation without stack overflows.

Even in rustc 1.44 allocating arrays too large to fit on the stack (say 8MB in size) will stack overflow even if the array is Boxed.

Basic usage:

use arr::Array;
// zero - fast allocation
let big_array: Array<u8> = Array::zero(1 << 25);
// default - slow allocation
let big_array2: Array<u8> = Array::new(1 << 25);
// template
let template = 10u8;
let big_array3: Array<u8> = Array::new_from_template(1 << 25, &template);

// Also works for 2d arrays (note even the sub-array would ordinarily blow stack)
let big_2d_array: Array<[u8; 1 << 25]> = Array::zero(4);

Try to do this with a traditional array:

let big_array: Box<[u8; 1 << 25]> = Box::new([0u8; 1 << 25]);

Currently the array supports three modes of allocation, via new (requires types to have Default + Copy) and new_from_template, only requiring Clone. The zero constructor uses an internal Zeroable trait only set for primitive types and their arrays. In the future this concept may be unsafely extended outside of this crate but for now it's private. As long as this type fits on the stack and the array fits in memory this should be allocatable.

No runtime deps