How can I get an array or a slice from a raw pointer?

夙愿已清 提交于 2019-11-27 07:07:22

问题


Can I somehow get an array from std::ptr::read?

I'd like to do something close to:

let mut v: Vec<u8> = ...
let view = &some_struct as *const _ as *const u8;
v.write(&std::ptr::read<[u8, ..30]>(view));

Which is not valid in this form (can't use the array signature).


回答1:


If you want to obtain a slice from a raw pointer, use std::slice::from_raw_parts():

let slice = unsafe { std::slice::from_raw_parts(some_pointer, count_of_items) };

If you want to obtain a mutable slice from a raw pointer, use std::slice::from_raw_parts_mut():

let slice = unsafe { std::slice::from_raw_parts_mut(some_pointer, count_of_items) };

Are you sure you want read()? Without special care it will cause disaster on structs with destructors. Also, read() does not read a value of some specified type from a pointer to bytes; it reads exactly one value of the type behind the pointer (e.g. if it is *const u8 then read() will read one byte) and returns it.

If you only want to write byte contents of a structure into a vector, you can obtain a slice from the raw pointer:

use std::mem;
use std::io::Write;

struct SomeStruct {
    a: i32,
}

fn main() {
    let some_struct = SomeStruct { a: 32 };

    let mut v: Vec<u8> = Vec::new();
    let view = &some_struct as *const _ as *const u8;
    let slice = unsafe { std::slice::from_raw_parts(view, mem::size_of::<SomeStruct>()) };
    v.write(slice).expect("Unable to write");

    println!("{:?}", v);
}

This makes your code platform-dependent and even compiler-dependent: if you use types of variable size (e.g. isize/usize) in your struct or if you don't use #[repr(C)], the data you wrote into the vector is likely to be read as garbage on another machine (and even #[repr(C)] may not lift this problem sometimes, as far as I remember).



来源:https://stackoverflow.com/questions/27150652/how-can-i-get-an-array-or-a-slice-from-a-raw-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!