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:
- call once with a null-pointer, last_os_error will be set to 122, required size written to an output pointer
- allocate a suitable buffer (I currently replace this with "check my struct is large enough")
- 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:
- allocate a box-of-bytes (
Box<[u8]>), pass that to the ffi call. ThenBox::into_raw, recast to DeviceDetails DST,Box::from_raw(like the Vec example on the transmute docs) - have
struct DeviceDetails(Box<[u8]>)and add an accessor methodpath()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.
The nomicon seems up-to-date on the topic but accurately claims that custom DSTs are a bit "half baked" right now
↩︎
4 posts - 3 participants
