https://github.com/upsuper/default-boxed
Helper trait to create a boxed instance without going through stack
https://github.com/upsuper/default-boxed
rust
Last synced: 4 months ago
JSON representation
Helper trait to create a boxed instance without going through stack
- Host: GitHub
- URL: https://github.com/upsuper/default-boxed
- Owner: upsuper
- License: mit
- Created: 2019-08-31T12:33:59.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-03-13T10:47:59.000Z (over 4 years ago)
- Last Synced: 2025-09-21T15:08:38.777Z (10 months ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 33.2 KB
- Stars: 36
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# default-boxed
[](https://github.com/upsuper/default-boxed/actions)
[](https://crates.io/crates/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`][copyless] or [`boxext`][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.
[copyless]: https://crates.io/crates/copyless
[boxext]: https://crates.io/crates/boxext
## Example
```rust
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);
```