13个版本
0.2.0 | 2022年3月13日 |
---|---|
0.1.11 | 2022年3月12日 |
0.1.10 | 2021年8月5日 |
0.1.9 | 2020年6月2日 |
0.1.1 | 2019年8月31日 |
#176 in 内存管理
671 每月下载量
用于 5 个crate (4 直接)
8KB
60 代码行
default-boxed
辅助特质,在堆上直接创建具有默认值的大型结构体实例,无需经过栈。
类似于不稳定 box
语法,它从语义上不需要在栈上创建整个结构体然后移动到堆上,因此与 copyless
或 boxext
不同,它不依赖于优化来消除在栈上构建结构体,这仍然可能在创建大型结构体时在调试构建时遇到栈溢出。
示例
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);
依赖项
~1.5MB
~34K SLoC