You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for Win32 variable-sized array fields #443
Hey Kenny Kerr (@kennykerr) and the whole team involved... this is Awesome!! I was really looking forward for this, great move by MS 👍
I was wondering if there's any idea in mind on how to deal with structs that last field is a unsized arrays (ANYSIZE_ARRAY), right now the way winapi-rs is handling this by declaring an array of size 1 (ex. CM_NOTIFY_EVENT_DATA.DeviceHandle.Data). Imho this is a bit of a constraint since we only have one field available which in the end leads to casting to thin pointers and memory transmuting operations -- specially when we receive a struct with this fields from a callback. I know this is a difficult case, and I'm pretty sure there's not straightforward way to solve it without some "unsafe" code but I would love to see/discuss your take on this 😃
P.S.: I'm aware there's already an issue (#423) that talks about arrays but I do think this case requires a separate discussion, feel free to close the issue if I'm wrong
Thanks n4r1B (@n4r1b)! Yes, this is a tricky one and worthy of discussion. It has arrays and unions - both problematic. When the variable-sized array is trailing, what I tend to do in C++ is to omit the field entirely and simply have an enclosing struct provide the data suitable to the application, assuming the alignment and padding are compatible. I agree an array of size 1 is not very useful but is sometimes the least awful approach. Another approach is illustrated in how I modeled windows::HString's internal header:
There is a fine line between too much and too little intervention by the code generator. I will leave this open and spend some time on this and other similar structs like OVERLAPPED that also tend to have trailing data.
The way windows::HString is modeled looks very promising tbh, I've never used MaybeUninit but seems like a good idea to keep the unitialized instance and avoid problems with unknown size at compilation time.
My only concern with MaybeUninit is, and again I've never used it before so I'm not aware of the possible implications:
Note that dropping a MaybeUninit will never call T's drop code. It is your responsibility to make sure T gets dropped if it got initialized.
Would this mean that an implicit call to drop will be needed to free the memory, could this potentially lead to memory leaks?
If I understand correctly what this issue is about, then another instance is the FILEGROUPDESCRIPTORA and FILEGROUPDESCRIPTORW structs (under Windows::Win32::UI::Shell).
What is currently the least bad way to access elements beyond the first one in an array field like this?
Activity
kennykerr commentedon Jan 24, 2021
Thanks n4r1B (@n4r1b)! Yes, this is a tricky one and worthy of discussion. It has arrays and unions - both problematic. When the variable-sized array is trailing, what I tend to do in C++ is to omit the field entirely and simply have an enclosing struct provide the data suitable to the application, assuming the alignment and padding are compatible. I agree an array of size 1 is not very useful but is sometimes the least awful approach. Another approach is illustrated in how I modeled
windows::HString's internal header:windows-rs/src/strings/hstring.rs
Lines 216 to 224 in 256538e
There is a fine line between too much and too little intervention by the code generator. I will leave this open and spend some time on this and other similar structs like
OVERLAPPEDthat also tend to have trailing data.n4r1b commentedon Jan 26, 2021
The way
windows::HStringis modeled looks very promising tbh, I've never usedMaybeUninitbut seems like a good idea to keep the unitialized instance and avoid problems with unknown size at compilation time.My only concern with
MaybeUninitis, and again I've never used it before so I'm not aware of the possible implications:Would this mean that an implicit call to drop will be needed to free the memory, could this potentially lead to memory leaks?
Correct me if I'm wrong but could it be sth like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=888b0db061878c7ad1bcd1989ada871c
kennykerr commentedon Mar 16, 2021
TOKEN_PRIVILEGESis another variant of an array field with a variable size. Just leaving that here to remind me to cover that.[-]Support for variable-sized array fields[/-][+]Support for Win32 variable-sized array fields[/+]djdisodo commentedon Jan 27, 2022
will const generics be good for this case?
mwcampbell commentedon Apr 12, 2022
If I understand correctly what this issue is about, then another instance is the
FILEGROUPDESCRIPTORAandFILEGROUPDESCRIPTORWstructs (underWindows::Win32::UI::Shell).What is currently the least bad way to access elements beyond the first one in an array field like this?
kennykerr commentedon Jul 22, 2022
Closing as duplicate of #907 which is blocked on microsoft/win32metadata#547