#stack-overflow #default-value #box #allocator #memcpy #debug-builds

no-std default-boxed

Helper trait to help create large struct on heap directly

13 releases

0.2.0 Mar 13, 2022
0.1.11 Mar 12, 2022
0.1.10 Aug 5, 2021
0.1.9 Jun 2, 2020
0.1.1 Aug 31, 2019

#192 in Memory management

Download history 62/week @ 2023-12-04 23/week @ 2023-12-11 29/week @ 2023-12-18 19/week @ 2023-12-25 8/week @ 2024-01-01 14/week @ 2024-01-08 18/week @ 2024-01-15 28/week @ 2024-01-22 118/week @ 2024-01-29 49/week @ 2024-02-05 82/week @ 2024-02-12 132/week @ 2024-02-19 135/week @ 2024-02-26 156/week @ 2024-03-04 190/week @ 2024-03-11 195/week @ 2024-03-18

683 downloads per month
Used in 5 crates (4 directly)

MIT license

8KB
60 lines

default-boxed

CI Crates.io

Helper trait to create instances of large structs with default value on heap directly without going through stack.

Similar to the unstable box syntax, it semantically doesn't require creating the whole struct on stack then moving to heap, and thus unlike copyless or boxext, it doesn't rely on optimization to eliminate building the struct on stack, which may still face stack overflow on debug build when creating large struct.

Example

use default_boxed::DefaultBoxed;

const BASE: usize = 1024;

#[derive(DefaultBoxed)]
struct Foo {
    a: Bar,
    b: [Bar; 1024 * BASE],
    c: [u32; 1024 * BASE],
}

struct Bar(u16);
impl Default for Bar {
    fn default() -> Bar {
        Bar(29)
    }
}

let foo = Foo::default_boxed();
assert_eq!(foo.a.0, 29);
assert_eq!(foo.b[128 * BASE].0, 29);
assert_eq!(foo.c[256 * BASE], 0);

let foo_arr = Foo::default_boxed_array::<16>();
assert_eq!(foo_arr[15].a.0, 29);

Dependencies

~1.5MB
~34K SLoC