将向量转换为数组并返回 [英] Convert vectors to arrays and back
问题描述
我正在尝试找出从向量转换为数组并返回的最类似于 Rust 的方式.这些宏可以工作,甚至可以用一些不安全的块来通用,但感觉都非常不像 Rust.
I am attempting to figure the most Rust-like way of converting from a vector to array and back. These macros will work and can even be made generic with some unsafe blocks, but it all feels very un-Rust like.
我将不胜感激任何输入并且不加打击,我认为这段代码远非很好或最佳.我现在只使用 Rust 几个星期,并在追逐版本和文档,所以非常感谢帮助.
I would appreciate any input and hold no punches, I think this code is far from nice or optimal. I have only played with Rust for a few weeks now and chasing releases and docs so really appreciate help.
macro_rules! convert_u8vec_to_array {
($container:ident, $size:expr) => {{
if $container.len() != $size {
None
} else {
use std::mem;
let mut arr : [_; $size] = unsafe { mem::uninitialized() };
for element in $container.into_iter().enumerate() {
let old_val = mem::replace(&mut arr[element.0],element.1);
unsafe { mem::forget(old_val) };
}
Some(arr)
}
}};
}
fn array_to_vec(arr: &[u8]) -> Vec<u8> {
let mut vector = Vec::new();
for i in arr.iter() {
vector.push(*i);
}
vector
}
fn vector_as_u8_4_array(vector: Vec<u8>) -> [u8;4] {
let mut arr = [0u8;4];
for i in (0..4) {
arr[i] = vector[i];
}
arr
}
推荐答案
代码对我来说似乎很好,尽管有一个非常重要的安全事项需要注意:当 arr
不是时,不能有恐慌t 完全初始化.在未初始化的内存上运行析构函数很容易导致未定义的行为,特别是这意味着 into_iter
和它的 next
方法不应该恐慌(我相信这是不可能的由于代码的约束,迭代器的 enumerate
和 mem::*
部分会发生恐慌).
The code seems fine to me, although there's a very important safety thing to note: there can be no panics while arr
isn't fully initialised. Running destructors on uninitialised memory could easily lead be undefined behaviour, and, in particular, this means that into_iter
and the next
method of it should never panic (I believe it is impossible for the enumerate
and mem::*
parts of the iterator to panic given the constraints of the code).
也就是说,可以用一个函数来表达 replace
/forget
习语:std::ptr::write
.
That said, one can express the replace
/forget
idiom with a single function: std::ptr::write
.
for (idx, element) in $container.into_iter().enumerate() {
ptr::write(&mut arr[idx], element);
}
虽然,我会把它写成:
for (place, element) in arr.iter_mut().zip($container.into_iter()) {
ptr::write(place, element);
}
同样,可以将一些迭代器的优点应用到 u8
专用版本:
Similarly, one can apply some iterator goodness to the u8
specialised versions:
fn array_to_vec(arr: &[u8]) -> Vec<u8> {
arr.iter().cloned().collect()
}
fn vector_as_u8_4_array(vector: Vec<u8>) -> [u8;4] {
let mut arr = [0u8;4];
for (place, element) in arr.iter_mut().zip(vector.iter()) {
*place = *element;
}
arr
}
虽然第一个可能更好地写成 arr.to_vec()
,第二个为
Although the first is probably better written as arr.to_vec()
, and the second as
let mut arr = [0u8; 4];
std::slice::bytes::copy_memory(&vector, &mut arr);
arr
虽然该功能目前不稳定,因此只能使用每晚.
Although that function is unstable currently, and hence only usable on nightly.
这篇关于将向量转换为数组并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!