Skip to content

Commit f70d381

Browse files
committed
Add new_boxed fn to AlignedBytes
Due to a long unfixed issue in Rust (rust-lang/rust#53827), initializing large arrays and boxing them causes a stack overflow. To avoid this, the new_boxed fn allocates itself manually.
1 parent ea018c0 commit f70d381

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/data_type/aligned_bytes.rs

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ where
2929
value: bytes,
3030
}
3131
}
32+
33+
/// Use this for large allocations better suited to the heap.
34+
///
35+
/// This fn also deals with [the boxed array issue in Rust](https://github.com/rust-lang/rust/issues/53827).
36+
pub fn new_boxed() -> Box<AlignedBytes<ALIGN, N>> {
37+
let layout = std::alloc::Layout::new::<AlignedBytes<ALIGN, N>>();
38+
unsafe {
39+
let ptr = std::alloc::alloc(layout) as *mut AlignedBytes<ALIGN, N>;
40+
Box::from_raw(ptr)
41+
}
42+
}
3243
}
3344

3445
impl<const ALIGN: usize, const N: usize> Default for AlignedBytes<ALIGN, N>

0 commit comments

Comments
 (0)