default-boxed

Crates.iodefault-boxed
lib.rsdefault-boxed
version0.2.0
sourcesrc
created_at2019-08-31 12:33:51.261505
updated_at2022-03-13 10:48:24.383218
descriptionHelper trait to help create large struct on heap directly
homepage
repositoryhttps://github.com/upsuper/default-boxed
max_upload_size
id161137
size13,249
Xidorn Quan (upsuper)

documentation

README

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);
Commit count: 51

cargo fmt