Beware that rustbindgen creates a binding to the C union with the same security as in C; as a result, when called:
event.xkey();
There is no runtime check that xkey is the field currently containing the value.
This is because, since C does not have a union label (i.e., union knowing which field is currently being used), the developers came up with various ways to encode this information (*), the ones I know of:
- external provider; usually another structure field immediately before
union - first field of each of the structures in
union
Here you are in the latter case int type; - This is the first field of the union, and each nested structure begins with int _type; to denote it. As a result, you need a two-step approach:
- consult
type() - depending on the value, call the correct reinterpretation
Mapping from the type value to the actual field used should be part of the C library documentation, hopefully.
I invite you to come up with a wrapper around this low-level union , which will make it safer to get the result. At least you can verify that it is the right type in the accessory; A complete approach is to come up with a Rust enum that will wrap proxies in all fields and allow pattern matching.
(*) and actually sometimes even ignore it, for example, on C99, in order to rethink a float as int a union { float f; int i; } union { float f; int i; } union { float f; int i; } .
source share