Zero-initialize a union in Rust

Viewed 131

Is there a reasonable way to zero-initialize a union data type in Rust?

The approach mentioned by the Rust Reference works well if the user initializes the largest field in the union, but doesn't work if a smaller field is set. For example, given a type:

union TypeFromFfi {
    small: u8,
    medium: u64,
    large: u128,
}

These don't seem to zero-initialize all of the union's memory:

let data = unsafe { TypeFromFfi { small: 0 } };
let data = unsafe { TypeFromFfi { medium: 0 } };

This seems to:

let data = unsafe { TypeFromFfi { large: 0 } };

This is as I expect, and I am hoping to avoid this approach because it requires the caller to remember the size of each field of the union.

Is there a general way to do zero-initialize a union? So far I have thought of:

let data = unsafe { std::mem::zeroed::<TypeFromFfi>() };

It seems to work, but is there something that I could be missing?

Could there be an issue with padding bytes from using std::mem::zeroed? I can't think of anything...

1 Answers
Related