使用宏初始化大量非复制元素
Using a macro to initialize a big array of non-Copy elements
我正在尝试使用相同的初始化程序初始化大量元素。 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
有没有什么方法可以在不剪切和粘贴每个初始化程序的情况下初始化这个数组?
这些宏的问题在于,前者在 Rust 中不会产生有效的语法形式——用逗号组合的两个表达式本身并不是有效的形式。它在另一个宏中 "injected" 进入方括号的事实是无关紧要的。
坦率地说,我不知道如何正确处理常规数组。缺少数字作为通用参数是一个众所周知的问题,它排除了许多有用的模式。例如,如果支持它们,就可以有这样的功能:
fn make_array<T, N: usize, F>(f: F) -> [T; N] where F: FnMut() -> T
创建一个任意大小的数组,用函数调用的结果填充它:
let array: [_; 64] = make_array(|| AllocatedMemory::<u8>{ mem: &mut [] })
但是,遗憾的是,Rust 中还没有这样的东西。您必须改用 Vec
之类的动态结构。您也可以尝试 arrayvec,它为某些固定大小的数组提供了类似于 Vec
的 API;使用它你可以做这样的事情:
use arrayvec::ArrayVec; // 0.5.1
fn main() {
let mut array = ArrayVec::<[_; 64]>::new();
for _ in 0..array.len() {
array.push(AllocatedMemory::<u8> { mem: &mut [] });
}
let array = array.into_inner(); // array: [AllocatedMemory<u8>; 64]
}
另请参阅:
- How do I collect into an array?
问题是the expansion of a macro absolutely must be a complete and independently valid grammar element。您不能扩展到 a, b
,就像您不能扩展到 42 +
。在 Rust 中也没有办法(静态地)连接或 cons 数组;整个数组初始化程序必须扩展到 一个 步骤。
这可以通过 push-down accumulation 使用宏来完成。诀窍是您将语法上尚未有效的部分数组表达式 down 传递给递归,而不是在返回的路上进行构造。当你到达扩展的底部时,你会立即发出现在完整的表达式。
这是一个支持长度为 0 到 8 的数组以及 2 的幂到 64 的宏:
macro_rules! array {
(@accum (0, $($_es:expr),*) -> ($($body:tt)*))
=> {array!(@as_expr [$($body)*])};
(@accum (1, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (0, $($es),*) -> ($($body)* $($es,)*))};
(@accum (2, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (0, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
(@accum (3, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (2, $($es),*) -> ($($body)* $($es,)*))};
(@accum (4, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (2, $($es,)* $($es),*) -> ($($body)*))};
(@accum (5, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)*))};
(@accum (6, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
(@accum (7, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)* $($es,)*))};
(@accum (8, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es,)* $($es),*) -> ($($body)*))};
(@accum (16, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (8, $($es,)* $($es),*) -> ($($body)*))};
(@accum (32, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (16, $($es,)* $($es),*) -> ($($body)*))};
(@accum (64, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (32, $($es,)* $($es),*) -> ($($body)*))};
(@as_expr $e:expr) => {$e};
[$e:expr; $n:tt] => { array!(@accum ($n, $e) -> ()) };
}
fn main() {
let ones: [i32; 64] = array![1; 64];
println!("{:?}", &ones[..]);
}
这里的策略是将输入的大小乘以 2 的幂,然后加上非 2 的幂的余数。这是为了通过确保 $n
快速下降值来避免宏递归限制(我相信默认值为 64)。
只是为了防止频繁的后续问题:否,你不能用算术来简化它;你不能在宏中做算术运算。 :)
附录:如果您不确定这是如何工作的,您可以在编译时将 -Z trace-macros
传递给 rustc
并查看每个宏调用展开。以 array![1; 6]
为例,你会得到这样的结果:
array! { 1 ; 6 }
array! { @ accum ( 6 , 1 ) -> ( ) }
array! { @ accum ( 4 , 1 ) -> ( 1 , 1 , ) }
array! { @ accum ( 2 , 1 , 1 ) -> ( 1 , 1 , ) }
array! { @ accum ( 0 , 1 , 1 ) -> ( 1 , 1 , 1 , 1 , 1 , 1 , ) }
array! { @ as_expr [ 1 , 1 , 1 , 1 , 1 , 1 , ] }
一个"safe" implementation which runs on stable, heavily inspired by Reddit:
// #![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 }));
// }
我正在尝试使用相同的初始化程序初始化大量元素。 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
有没有什么方法可以在不剪切和粘贴每个初始化程序的情况下初始化这个数组?
这些宏的问题在于,前者在 Rust 中不会产生有效的语法形式——用逗号组合的两个表达式本身并不是有效的形式。它在另一个宏中 "injected" 进入方括号的事实是无关紧要的。
坦率地说,我不知道如何正确处理常规数组。缺少数字作为通用参数是一个众所周知的问题,它排除了许多有用的模式。例如,如果支持它们,就可以有这样的功能:
fn make_array<T, N: usize, F>(f: F) -> [T; N] where F: FnMut() -> T
创建一个任意大小的数组,用函数调用的结果填充它:
let array: [_; 64] = make_array(|| AllocatedMemory::<u8>{ mem: &mut [] })
但是,遗憾的是,Rust 中还没有这样的东西。您必须改用 Vec
之类的动态结构。您也可以尝试 arrayvec,它为某些固定大小的数组提供了类似于 Vec
的 API;使用它你可以做这样的事情:
use arrayvec::ArrayVec; // 0.5.1
fn main() {
let mut array = ArrayVec::<[_; 64]>::new();
for _ in 0..array.len() {
array.push(AllocatedMemory::<u8> { mem: &mut [] });
}
let array = array.into_inner(); // array: [AllocatedMemory<u8>; 64]
}
另请参阅:
- How do I collect into an array?
问题是the expansion of a macro absolutely must be a complete and independently valid grammar element。您不能扩展到 a, b
,就像您不能扩展到 42 +
。在 Rust 中也没有办法(静态地)连接或 cons 数组;整个数组初始化程序必须扩展到 一个 步骤。
这可以通过 push-down accumulation 使用宏来完成。诀窍是您将语法上尚未有效的部分数组表达式 down 传递给递归,而不是在返回的路上进行构造。当你到达扩展的底部时,你会立即发出现在完整的表达式。
这是一个支持长度为 0 到 8 的数组以及 2 的幂到 64 的宏:
macro_rules! array {
(@accum (0, $($_es:expr),*) -> ($($body:tt)*))
=> {array!(@as_expr [$($body)*])};
(@accum (1, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (0, $($es),*) -> ($($body)* $($es,)*))};
(@accum (2, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (0, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
(@accum (3, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (2, $($es),*) -> ($($body)* $($es,)*))};
(@accum (4, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (2, $($es,)* $($es),*) -> ($($body)*))};
(@accum (5, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)*))};
(@accum (6, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)*))};
(@accum (7, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es),*) -> ($($body)* $($es,)* $($es,)* $($es,)*))};
(@accum (8, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (4, $($es,)* $($es),*) -> ($($body)*))};
(@accum (16, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (8, $($es,)* $($es),*) -> ($($body)*))};
(@accum (32, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (16, $($es,)* $($es),*) -> ($($body)*))};
(@accum (64, $($es:expr),*) -> ($($body:tt)*))
=> {array!(@accum (32, $($es,)* $($es),*) -> ($($body)*))};
(@as_expr $e:expr) => {$e};
[$e:expr; $n:tt] => { array!(@accum ($n, $e) -> ()) };
}
fn main() {
let ones: [i32; 64] = array![1; 64];
println!("{:?}", &ones[..]);
}
这里的策略是将输入的大小乘以 2 的幂,然后加上非 2 的幂的余数。这是为了通过确保 $n
快速下降值来避免宏递归限制(我相信默认值为 64)。
只是为了防止频繁的后续问题:否,你不能用算术来简化它;你不能在宏中做算术运算。 :)
附录:如果您不确定这是如何工作的,您可以在编译时将 -Z trace-macros
传递给 rustc
并查看每个宏调用展开。以 array![1; 6]
为例,你会得到这样的结果:
array! { 1 ; 6 }
array! { @ accum ( 6 , 1 ) -> ( ) }
array! { @ accum ( 4 , 1 ) -> ( 1 , 1 , ) }
array! { @ accum ( 2 , 1 , 1 ) -> ( 1 , 1 , ) }
array! { @ accum ( 0 , 1 , 1 ) -> ( 1 , 1 , 1 , 1 , 1 , 1 , ) }
array! { @ as_expr [ 1 , 1 , 1 , 1 , 1 , 1 , ] }
一个"safe" implementation which runs on stable, heavily inspired by Reddit:
// #![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 }));
// }