Skip to content

extern interop of tagged unions? #17767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
expikr opened this issue Oct 28, 2023 · 1 comment
Closed

extern interop of tagged unions? #17767

expikr opened this issue Oct 28, 2023 · 1 comment

Comments

@expikr
Copy link
Contributor

expikr commented Oct 28, 2023

On Windows there are a lot of functions where you provide buffer and the system writes a struct to it that are essentially a tagged union:

typedef struct tagSOMEDATA {
    UINT cbSize;
    DWORD dwType;
    union {
        variant0: SOMEVARIANT0;
        variant1: SOMEVARIANT1;
        variant2: SOMEVARIANT2;
    } data;
} SOMEDATA, *PSOMEDATA

This is essentially a union(enum(u32)) that you receive, it would be a neat QoL to just declare the extern interop type as such from the start rather than manually checking the type field and returning your custom Ziggified tagged union to wrap it.

Perhaps something like

const SOMEDATA = extern struct {
    cbSize: u32,
    dwType: enum(u32){variant0,variant1,variant2},
    data: extern union (.dwType) {
        variant0: SOMEVARIANT0;
        variant1: SOMEVARIANT1;
        variant2: SOMEVARIANT2;
    }
}

But then that would have the problem of being able to mutate the type field separately from the data field, so not sure what the right solution would be here.

Another consideration is the use of anonymous unions, i.e. DUMMYUNIONNAME, which extracts their fields into the parent scope, e.g.

typedef struct tagSOMEDATA {
    UINT cbSize;
    DWORD dwType;
    union {
        variant0: SOMEVARIANT0;
        variant1: SOMEVARIANT1;
        variant2: SOMEVARIANT2;
    } DUMMYUNIONNAME; // anonymous union
} SOMEDATA, *PSOMEDATA

SOMEDATA mydata = {
    .cbSize = sizeof(SOMEDATA);
    .dwType = 0;
    .variant1 = myVariant1Details; // treated as if the union variant is in parent scope
}
@BratishkaErik
Copy link
Contributor

BratishkaErik commented Oct 28, 2023

Duplicate of #1072 ?

@expikr expikr closed this as completed Mar 29, 2024
@expikr expikr closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants