Skip to content

Support for Win32 variable-sized array fields #443

Closed

Description

@n4r1b

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 😃

BTW, this was already discussed in the internal forums https://internals.rust-lang.org/t/ffi-types-with-unsized-arrays/5203/2

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

Activity

  1. kennykerr commented on Jan 24, 2021

    @kennykerr
    Collaborator

    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:

    #[repr(C)]
    pub struct Header {
    flags: u32,
    len: u32,
    _0: u32,
    _1: u32,
    data: *mut u16,
    shared: std::mem::MaybeUninit<Shared>,
    }

    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.

  2. n4r1b commented on Jan 26, 2021

    @n4r1b
    Author

    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?

    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

  3. kennykerr commented on Mar 16, 2021

    @kennykerr
    Collaborator

    TOKEN_PRIVILEGES is another variant of an array field with a variable size. Just leaving that here to remind me to cover that.

  4. changed the title [-]Support for variable-sized array fields[/-] [+]Support for Win32 variable-sized array fields[/+] on Mar 16, 2021
  5. djdisodo commented on Jan 27, 2022

    @djdisodo

    will const generics be good for this case?

    #[repr(C)]
    pub struct TOKEN_PRIVILEGES<const SIZE: usize> {
        pub PrivilegeCount: u32,
        pub Privileges: [LUID_AND_ATTRIBUTES; SIZE],
    }
  6. mwcampbell commented on Apr 12, 2022

    @mwcampbell

    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?

  7. kennykerr commented on Jul 22, 2022

    @kennykerr
    Collaborator

    Closing as duplicate of #907 which is blocked on microsoft/win32metadata#547

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions