[][src]Crate default_boxed

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;

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

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

#[test]
fn test_basic() {
    let foo = Foo::default_boxed();
    assert_eq!(foo.a.0, 29);
    assert_eq!(foo.b[128 * 1024].0, 29);
    assert_eq!(foo.c[256 * 1024], 0);
}

Traits

DefaultBoxed

Helper trait to create a boxed instance of the given type with a default value for each field.

Derive Macros

DefaultBoxed