This new problem is relevant to my topic on reading structs from raw memory. I was able to solve that problem, however I'm now encountering a new problem, which is why I thought I'd post it in a new topic. These lines of my code:
let cmdfis = unsafe {
let raw_ptr = cmdtbl.cfis as *mut FisRegH2D;
raw_ptr.as_mut().unwrap() as &mut FisRegH2D
};
Fails with error E0605. cfis is:
pub cfis: [cty::c_uchar; 64usize],
I want it to be converted into this:
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct FisRegH2D {
pub fis_type: cty::c_uchar,
_bitfield_1: internal::bitfield<[u8; 1usize], u8>,
pub command: cty::c_uchar,
pub feature_lo: cty::c_uchar,
pub lba0: cty::c_uchar,
pub lba1: cty::c_uchar,
pub lba2: cty::c_uchar,
pub device: cty::c_uchar,
pub lba3: cty::c_uchar,
pub lba4: cty::c_uchar,
pub lba5: cty::c_uchar,
pub feature_hi: cty::c_uchar,
pub count_lo: cty::c_uchar,
pub count_hi: cty::c_uchar,
pub icc: cty::c_uchar,
pub control: cty::c_uchar,
rsv1: [cty::c_uchar; 4usize],
}
I would use slice_from_raw_parts_mut and/or slice_from_raw_parts, but I don't think that would work (after the pointer retrieval is done, I need to use the returned reference as a struct). Rustc recommends I implement a type conversion, but I have absolutely no idea how I would even do that (i.e. which elements of the array would map to which struct members). or reference, the error I get is this:
error[E0605]: non-primitive cast: `[u8; 64]` as `*mut drivers::storage::ahci::FisRegH2D`
--> src\drivers\storage\ahci.rs:1227:15
|
1227 | let raw_ptr = cmdtbl.cfis as *mut FisRegH2D;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
Issue is that I don't know how I could apply the from
trait to work this way. In C, I know that I can just declare an array, typecast it into a structure and then access it as though it had been a struct all along. I like rust though and so am wondering if there's a way to do something like this (but maybe in a safer way)?