Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Translating C Unions

Lander Brandt edited this page Jul 13, 2019 · 1 revision

Since unions in Rust require usage of unsafe code, "unions" for lain are implemented using enums with non-unit variants. The following is an example of a union in C and its corresponding representation for lain:

typedef union _Foo {
    struct {
        uint32_t a;
        uint32_t b;
    };
    uint64_t foo;
} Foo;
#[derive(NewFuzzed, BinarySerialize)]
struct FooParts {
    a: u32,
    b: u32,
}

#[derive(NewFuzzed, BinarySerialize)]
#[serialized_size(8)]
enum Foo {
    AB(FooParts),
    AsU64(u64),
}

For more information on the attributes and derives used, see the Derives section.

Clone this wiki locally