Box<[u8; CONST_SIZE]> initialization

有几种实现方法:

let mut buf: Box<[u8; 4096]> = unsafe {
    alloc::boxed::Box::new_zeroed().assume_init()
};
let mut buf: Box<[u8; 4096]> = vec![0u8; 4096]
    .into_boxed_slice()
    .try_into()
    .expect("Failed to convert boxed slice to boxed array");
let mut buf: Box<[u8;4096] = Box::new([u8;4096]);

其中第三种会 clippy 提示在栈上超过了4096,但按道理在下面已经添加了#[rustc_box],不会再在栈上生成临时变量:

impl<T> Box<T> {
    /// Allocates memory on the heap and then places `x` into it.
    ///
    /// This doesn't actually allocate if `T` is zero-sized.
    ///
    /// # Examples
    ///
    /// ```
    /// let five = Box::new(5);
    /// ```
    #[cfg(not(no_global_oom_handling))]
    #[inline(always)]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[must_use]
    #[rustc_diagnostic_item = "box_new"]
    #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
    pub fn new(x: T) -> Self {
        #[rustc_box]
        Box::new(x)
    }
// box $expr
let p = alloc(Layout::new::<typeof!($expr)>());
p.write($expr);
p

// Box::new($expr)
let tmp = $expr;
let p = alloc(Layout::new::<typeof!(tmp)>());
p.write(tmp);
p