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

后端 未结 1 1005
说谎
说谎 2020-12-11 04:54

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

I\'d like to do something close to:

let mut v: Vec = ...
let view = &some_         


        
相关标签:
1条回答
  • 2020-12-11 05:27

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

    0 讨论(0)
提交回复
热议问题