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).
If you want to obtain a slice from a raw pointer, use
std::slice::from_raw_parts()
:If you want to obtain a mutable slice from a raw pointer, use
std::slice::from_raw_parts_mut()
: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
thenread()
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:
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).