Introduce `Box::new_uninit_array` and `Box::new_zeroed_array`
Currently the Box type has the methods Box::new_uninit_slice(len: usize) and Box::new_zeroed_slice(len: usize), which create Box<[MaybeUninit<T>]> with uninitialized and zeroed memory respectively.
I propose the creation of Box::new_uninit_array() and Box::new_zeroed_array(), which create Box<[MaybeUninit<T>; N]>, which create fixed-size arrays on the heap with a compile time size.
One example from my current use case (an I/O buffer):
struct Buffer<const N: usize> {
data: Box<[MaybeUninit<u8>; N]>,
...
}
impl<const N: usize> Buffer<N> {
pub fn new() -> Self {
let data = Box::new_uninit_array();
....
Self {
data,
...
}
}
}
To my knowledge, the only way to do this currently is:
Box::new_uninit_slice(N).into_array().unwrap()
...which is both unstable and somewhat unsightly in my opinion with an unwrap from an unnecessary runtime check.
I would love to take a shot at making a PR myself, but I thought I should post here first before I did anything, I'm not sure of the exact process as this would be my first contribution to Rust. If anyone here has any objections or pointers please let me know.
Discussion in the ATmosphere