corrode
corrode copied to clipboard
implement union
C file:
// test.c
#include <stdio.h>
struct Fish {
char type[];
int age;
} fish;
I run ~$corrode test.c
and get
corrode: ("/usr/include/wchar.h": line 85): Corrode doesn't handle this yet:
union {
unsigned int __wch; char __wchb[4];
}
Any tips on why this has happened?
For one thing, Rust is still in the process of implementing support for untagged unions in rust-lang/rust#32836.
Yeah, since Rust doesn't support C-style unions quite yet, Corrode doesn't either. Thanks @dikaiosune for the pointer to the work in progress there!
I'm going to repurpose this issue as a tracking bug for Corrode's current lack of support for unions, since I hadn't filed one about that defect yet.
Other advice on your specific test case, @Cortlandd:
Unfortunately, a lot of standard system headers declare unions, or enums, or other things Corrode doesn't support yet. For testing purposes, try writing source files that don't
#include
anything, and just copy the declarations you need. For example, if you want to callprintf
, use this instead of#include <stdio.h>
:
extern int printf (const char *format, ...);
You should also verify that your C source compiles with a normal C compiler like gcc or clang. I think your
char type[];
field is not legal C. If I'm not mistaken, an array field with no size specified can only appear at the end of a struct, right? At any rate, Corrode currently translates arrays incorrectly, treating them as if they were just pointers.
Thanks! This is helpful
I don't actually want to close this issue until Corrode has support for unions. :smile:
shall we use union implementation Just as rust-bindgen did; rust-bindgen support union and enum
and we can pass union and enum error as a replacement
Is it possible to have a workaround until rust supports union? Maybe use enumerations somehow?
As a first step, Corrode now translates union types to non-constructable and non-copyable enums. This supports any code that only passes around pointers to union types, but doesn't try to access the contents of any union.
In particular, stdio.h
translates without Corrode complaining now! However the generated Rust doesn't compile for other reasons.
I won't close this issue until Corrode translates unions to something complete on the Rust side, but for now this should let people translate a lot more code successfully.
I'm at RustConf today and @joshtriplett tells me that support for C-style unions has landed on Rust nightly. Hooray! Anyone want to try making Corrode use the new union syntax?