From f70d3816c12bc9c812e0e66949a2578a5aeaa7c7 Mon Sep 17 00:00:00 2001 From: prof64 <80442784+prof64@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:55:06 -0400 Subject: [PATCH] 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. --- src/data_type/aligned_bytes.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/data_type/aligned_bytes.rs b/src/data_type/aligned_bytes.rs index dc43ad4..72abc18 100644 --- a/src/data_type/aligned_bytes.rs +++ b/src/data_type/aligned_bytes.rs @@ -29,6 +29,17 @@ where value: bytes, } } + + /// Use this for large allocations better suited to the heap. + /// + /// This fn also deals with [the boxed array issue in Rust](https://github.com/rust-lang/rust/issues/53827). + pub fn new_boxed() -> Box> { + let layout = std::alloc::Layout::new::>(); + unsafe { + let ptr = std::alloc::alloc(layout) as *mut AlignedBytes; + Box::from_raw(ptr) + } + } } impl Default for AlignedBytes