Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

unsafe

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).

like image 211
viraptor Avatar asked Nov 26 '14 13:11

viraptor


1 Answers

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).

like image 146
Vladimir Matveev Avatar answered Nov 04 '22 22:11

Vladimir Matveev