我正在尝试用相同的初始化器初始化一个很大的元素数组。64个元素只是一个例子--我希望它至少是16k。不幸的是,一个简单的
let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []};64];
由于AllocatedMemory
结构不实现Copy
,因此无法工作
error: the trait `core::marker::Copy` is not implemented for the type `AllocatedMemory<'_, u8>` [E0277]
let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []}; 64];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
所以我尝试了宏,但没有用:
struct AllocatedMemory<'a, T: 'a> {
mem: &'a mut [T],
}
macro_rules! init_memory_helper {
(1, $T : ty) => { AllocatedMemory::<$T>{mem: &mut []} };
(2, $T : ty) => { init_memory_helper!(1, $T), init_memory_helper!(1, $T) };
(4, $T : ty) => { init_memory_helper!(2, $T), init_memory_helper!(2, $T) };
(8, $T : ty) => { init_memory_helper!(4, $T), init_memory_helper!(4, $T) };
(16, $T : ty) => { init_memory_helper!(8, $T), init_memory_helper!(8, $T) };
(32, $T : ty) => { init_memory_helper!(16, $T), init_memory_helper!(16, $T) };
(64, $T : ty) => { init_memory_helper!(32, $T), init_memory_helper!(32, $T) };
}
macro_rules! init_memory {
(1, $T : ty) => { [init_memory_helper!(1, $T)] };
(2, $T : ty) => { [init_memory_helper!(2, $T)] };
(4, $T : ty) => { [init_memory_helper!(4, $T)] };
(8, $T : ty) => { [init_memory_helper!(8, $T)] };
(16, $T : ty) => { [init_memory_helper!(16, $T)] };
(32, $T : ty) => { [init_memory_helper!(32, $T)] };
(64, $T : ty) => { [init_memory_helper!(64, $T)] };
}
fn main() {
let array: [AllocatedMemory<u8>; 64] = init_memory!(64, u8);
println!("{:?}", array[0].mem.len());
}
错误消息是
error: macro expansion ignores token `,` and any following
(64, $T : ty) => { init_memory_helper!(32, $T), init_memory_helper!(32, $T) };
note: caused by the macro expansion here; the usage of `init_memory_helper!` is likely invalid in expression context
有没有办法在不剪切和粘贴每个初始化器的情况下初始化这个数组?
发布于 2020-01-08 00:13:11
受Reddit启发运行在稳定平台上的"safe" implementation
// #![feature(core_intrinsics)]
// use std::ptr;
use std::mem;
use std::mem::MaybeUninit;
type MyStructValue = Vec<usize>;
type UsizeToVecBuilder = Box<dyn Fn(usize) -> Vec<usize>>;
#[derive(Debug)]
struct MyStruct {
value: MyStructValue,
}
macro_rules! make_array {
([$t:ident; $n:expr], $constructor:expr, $builder:expr) => {{
let mut data: [MaybeUninit<$t>; $n] = unsafe { MaybeUninit::uninit().assume_init() };
let mut i: usize = 0;
for elem in &mut data[..] {
*elem = MaybeUninit::new($constructor(i, $builder));
i += 1;
}
unsafe { mem::transmute::<_, [$t; $n]>(data) }
}};
}
fn main() {
println!(
"{:?}",
make_array!(
[MyStruct; 5],
|i, b: UsizeToVecBuilder| MyStruct { value: b(i) },
Box::new(|i| (0..i + 1).collect())
)
);
}
// unstable version: (see reddit: https://www.reddit.com/r/rust/comments/29ymbx/a_macro_to_fill_a_fixed_length_array/)
//
// macro_rules! make_array {
// ($n:expr, $constructor:expr) => {{
// let mut items: [_; $n] = unsafe { mem::uninitialized() };
// for i in 0..$n {
// let val = $constructor(i);
// unsafe {
// std::intrinsics::volatile_copy_nonoverlapping_memory(
// &mut items[i], &val, 1
// );
// // ptr::copy_nonoverlapping_memory(&mut items[i], &val, 1);
// mem::forget(val);
// }
// }
// items
// }}
// }
// fn main() {
// unstable version:
// println!("{:?}", make_array!(5, |i| MyStruct { value: i }));
// }
https://stackoverflow.com/questions/36258417
复制相似问题