如何创建包含大型数组的结构数组? [英] How to create an array of structs containing large arrays?
问题描述
如何创建包含固定大小的大数组的结构数组?我想使用数组而不是向量。
How could I create an array of structs containing big arrays with a fixed size? I want to use arrays and not vectors.
此代码是一个示例,但不编译
This code is an example but doesn't compile
struct _Tmove {
data1: usize,
data2: u64,
data3: bool,
}
struct _TmoveP {
data4: Box<[_Tmove]>,
data5: isize,
}
fn main() {
let mut gen_list = Box::new([
_TmoveP {
data5: 1,
data4: Box::new([_Tmove { data1: 5, data2: 1, data3: true }; 300]),
}
; 100000]);
assert!(gen_list[0].data4[0].data1==5);
}
error[E0277]: the trait bound `_Tmove: std::marker::Copy` is not satisfied
--> src/main.rs:16:29
|
16 | data4: Box::new([_Tmove { data1: 5, data2: 1, data3: true }; 300]),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the `Copy` trait is required because the repeated element will be copied
error[E0277]: the trait bound `_TmoveP: std::marker::Copy` is not satisfied
--> src/main.rs:13:33
|
13 | let mut gen_list = Box::new([
| ^
|
= note: the `Copy` trait is required because the repeated element will be copied
我正在使用Rust 1.12。
I am using Rust 1.12.
推荐答案
为了受益于初始化语法: [expr; N]
,需要将 expr
的结果复制
(因为需要制作副本)。
In order to benefit from the initialization syntax: [expr; N]
, the result of expr
need to be Copy
(as copies need be made).
#[derive(Copy, Clone)]
struct _Tmove {
data1: usize,
data2: u64,
data3: bool,
}
#[derive(Copy, Clone)]
struct _TmoveP {
data4: Box<[_Tmove]>,
data5: isize,
}
但是,在这种情况下, _TmoveP
不能 Copy
,因为它包含一个 Box
,而不是 Copy
。
However, in this case, _TmoveP
cannot be Copy
because it contains a Box
which is not Copy
.
好的,让我们摆脱 Box
:
#[derive(Copy, Clone)]
struct _TmoveP {
data4: [_Tmove; 300],
data5: isize,
}
听起来很棒吗?
但不幸的是, [_ Tmove; 300]
也不是 Clone
之一:(很遗憾,我们遇到了Rust编译器的限制(它的大小小于32)。
But unfortunately, [_Tmove; 300]
is not Clone
either :( We are unfortunately hitting a limitation of the Rust compiler (it works for size less than 32).
复制
很容易...但是首先我们必须实现 Clone
天真的方法很有趣,但是很简单:
The Copy
is easy enough... but first we have to implement Clone
manually. The naive way is no fun, but it's easy enough:
impl Clone for _TmoveP {
fn clone(&self) -> _TmoveP {
unsafe {
let mut res = _TmoveP {
data4: std::mem::uninitialized(),
data5: self.data5,
};
std::ptr::copy_nonoverlapping(
&self.data4 as *const _Tmove,
std::mem::transmute(&mut res.data4),
300
);
res
}
}
}
注意:出于某些原因,& mut res.data4为* mut _
无法编译...无论:x
Note: for some reason &mut res.data4 as *mut _
would not compile... whatever :x
但是, @FrancisGagné在评论中提醒我, Copy
类型有一个怪异的窍门:
However, @Francis Gagné reminded me in the comments, there is a weird trick with Copy
types:
impl Clone for _TmoveP {
fn clone(&self) -> _TmoveP { *self }
}
此方法由于某些原因很方便
This works, for some reason, and is handy in those situations.
最后,这可行...哦,等等, main
中有问题!
And, finally, this works... oh wait, there's an issue in main
!
fn main() {
let gen_list = Box::new([
_TmoveP {
data5: 1,
data4: [_Tmove { data1: 5, data2: 1, data3: true }; 300],
}
; 100000]);
assert!(gen_list[0].data4[0].data1==5);
}
好的,我们到这里。
仅对小于32的大小真正起作用的数组有什么处理?
What's the deal with arrays only really working for sizes smaller than 32?
简单地说:Rust还没有(现在?)对non的支持。类型的通用参数。
Simply put: Rust does not have (yet?) support for non-type generic parameters.
数组在某种程度上是特殊情况,但需要独立地为每个大小实现特征...因此标准库为数组实现了其特征到32号,因为这似乎是一个不错的权衡。
Arrays are special-cased, to an extent, but require implementing traits for each size independently... so the standard library implements its traits for arrays up to size 32, because it seemed a good trade-off.
这篇关于如何创建包含大型数组的结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!