9 releases

0.2.0 Mar 13, 2022
0.1.7 Mar 12, 2022
0.1.6 Jun 3, 2020
0.1.4 Sep 1, 2019
0.1.1 Aug 31, 2019

#6 in #boxed

Download history 46/week @ 2023-11-26 64/week @ 2023-12-03 28/week @ 2023-12-10 35/week @ 2023-12-17 23/week @ 2023-12-24 9/week @ 2023-12-31 15/week @ 2024-01-07 20/week @ 2024-01-14 33/week @ 2024-01-21 118/week @ 2024-01-28 53/week @ 2024-02-04 71/week @ 2024-02-11 126/week @ 2024-02-18 133/week @ 2024-02-25 144/week @ 2024-03-03 92/week @ 2024-03-10

502 downloads per month
Used in 6 crates (via default-boxed)

MIT license

5KB
87 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