28 releases

0.13.2 Oct 4, 2025
0.13.0 Jan 9, 2025
0.12.0 Jan 8, 2025
0.11.0 Sep 29, 2024
0.1.1 Mar 29, 2022

#45 in Memory management

Download history 128950/week @ 2025-11-27 135458/week @ 2025-12-04 120745/week @ 2025-12-11 102249/week @ 2025-12-18 79919/week @ 2025-12-25 114175/week @ 2026-01-01 155601/week @ 2026-01-08 165531/week @ 2026-01-15 219200/week @ 2026-01-22 271053/week @ 2026-01-29 289182/week @ 2026-02-05 299575/week @ 2026-02-12 319504/week @ 2026-02-19 575659/week @ 2026-02-26 423654/week @ 2026-03-05 402911/week @ 2026-03-12

1,779,408 downloads per month
Used in 981 crates (46 directly)

MIT license

86KB
2K SLoC

dynstack

Stack that allows users to allocate dynamically sized arrays.

The stack wraps a buffer of bytes that it uses as a workspace. Allocating an array takes a chunk of memory from the stack, which can be reused once the array is dropped.

Features

  • alloc: enables the allocator api.

Examples

use core::mem::MaybeUninit;
use dynstack::{DynStack, StackReq};

// We allocate enough storage for 3 `i32` and 4 `u8`.
let mut buf = [MaybeUninit::uninit();
    StackReq::new::<i32>(3)
        .and(StackReq::new::<u8>(4))
        .unaligned_bytes_required()];
let stack = DynStack::new(&mut buf);

{
    // We can have nested allocations.
    // 3×`i32`
    let (array_i32, substack) = stack.make_with::<i32>(3, |i| i as i32);
    // and 4×`u8`
    let (mut array_u8, _) = substack.make_with::<u8>(4, |_| 0);

    // We can read from the arrays,
    assert_eq!(array_i32[0], 0);
    assert_eq!(array_i32[1], 1);
    assert_eq!(array_i32[2], 2);

    // and write to them.
    array_u8[0] = 1;

    assert_eq!(array_u8[0], 1);
    assert_eq!(array_u8[1], 0);
    assert_eq!(array_u8[2], 0);
    assert_eq!(array_u8[3], 0);
}

{
    // We can also have disjoint allocations.
    // 3×`i32`
    let (mut array_i32, _) = stack.make_with::<i32>(3, |i| i as i32);
    assert_eq!(array_i32[0], 0);
    assert_eq!(array_i32[1], 1);
    assert_eq!(array_i32[2], 2);
}

{
    // or 4×`u8`
    let (mut array_u8, _) = stack.make_with::<i32>(4, |i| i as i32 + 3);
    assert_eq!(array_u8[0], 3);
    assert_eq!(array_u8[1], 4);
    assert_eq!(array_u8[2], 5);
    assert_eq!(array_u8[3], 6);
}

Dependencies

~140KB