The next step I'm about to take in polishing redbook is to find a better way to handle one of the ffi calls which returns a DST[1].

The "correct" definition of the struct would be:

pub(crate) struct DeviceDetails { cbSize: u32, DevicePath: [u16], } 

I currently have the workaround, that relies on assumed knowledge of the maximum likely path length:

pub(crate) struct DeviceDetails { cbSize: u32 = const {size_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_W>() as u32}, DevicePath: [u16; MAX_PATH_CHARS] = [0; _], } 

The ffi provides a call form which returns the required size of DevicePath, so I can obtain a value at runtime, via is the recommended approach:

  1. call once with a null-pointer, last_os_error will be set to 122, required size written to an output pointer
  2. allocate a suitable buffer (I currently replace this with "check my struct is large enough")
  3. call again providing a *mut to the buffer, to get real data back

I'd like to move away from my "best guess or panic" approach and am wondering which pattern is least not-sane:

  1. allocate a box-of-bytes (Box<[u8]>), pass that to the ffi call. Then Box::into_raw, recast to DeviceDetails DST, Box::from_raw (like the Vec example on the transmute docs)
  2. have struct DeviceDetails(Box<[u8]>) and add an accessor method path() which reads the relevant bytes from the bag. I have a struct available via bindgen which gives me reliable layout details (but only has a [u16; 1] at the tail)

I'm going to start working on option 1 for the afternoon, but wanted to ask for experience and advice here while I do that.


  1. The nomicon seems up-to-date on the topic but accurately claims that custom DSTs are a bit "half baked" right now :wink: ↩︎

4 posts - 3 participants

Read full topic