标签: maybeuninit

创建一个 `Pin<Box<[T; N]>>` 在 Rust 中,当 `[T; N]`太大,无法在堆栈上创建

广义问题

如何pinned_array_of_default在稳定的 Rust 中实现一个[T; N]太大而无法放入堆栈的通用函数?

fn pinned_array_of_default<T: Default, const N: usize>() -> Pin<Box<[T; N]>> {
    unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)

或者,如果这样可以使过程更容易,则T可以实施。Copy

fn pinned_array_of_element<T: Copy, const N: usize>(x: T) -> Pin<Box<[T; N]>> {
    unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)

将解决方案保存在安全的 Rust 中会更好,但似乎不太可能。

方法

最初,我希望通过实现,Default我也许能够Default处理初始分配,但是它仍然在堆栈上创建它,因此这对于较大的 值不起作用N

let boxed: Box<[T; N]> = Box::default();
let foo = Pin::new(boxed);
Run Code Online (Sandbox Code Playgroud)

我怀疑我需要使用它MaybeUninit来实现这一点,并且有一个Box::new_uninit()函数,但它目前不稳定,我理想地希望将其保留在稳定的 Rust 中。我也有点不确定转变为Pin<Box<MaybeUninit<B>>>是否Pin<Box<B>>会对Pin.

背景

使用 a 的目的Pin<Box<[T; N]>>是保存一个指针块,其中 …

allocation rust maybeuninit

1
推荐指数
1
解决办法
407
查看次数

标签 统计

allocation ×1

maybeuninit ×1

rust ×1