Stream: t-lang/wg-unsafe-code-guidelines

Topic: union


Simon Sapin (Aug 20 2019 at 12:38, on Zulip):

@RalfJ Does this looks like a bad idea or a terrible idea? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=72153dbf42418f6de937b4ed55dc93eb

Lokathor (Aug 20 2019 at 15:21, on Zulip):

Well, I've seen worse ideas.

gnzlbg (Aug 21 2019 at 13:24, on Zulip):

Why

gnzlbg (Aug 21 2019 at 13:25, on Zulip):

I mean, that doesn't really buy you anything over a #[repr(C)] union

gnzlbg (Aug 21 2019 at 13:25, on Zulip):

If it's just for fun, or to support older Rust toolchains without unions, then sure

gnzlbg (Aug 21 2019 at 13:25, on Zulip):

but since you are using MaybeUninit..

rkruppe (Aug 21 2019 at 13:36, on Zulip):

My first reaction was similar, but the padding-related ABI headaches w.r.t. unions wouldn't apply the a struct containing a big MaybeUninit.

gnzlbg (Aug 21 2019 at 14:05, on Zulip):

you can add an extra field to the union to avoid those headaches

gnzlbg (Aug 21 2019 at 14:05, on Zulip):

(e.g. () or [u8; size_of::<Self>()])

Thom Chiovoloni (Aug 21 2019 at 16:26, on Zulip):

do unions still not support non-Copy types on stable? This would.

RalfJ (Aug 24 2019 at 11:15, on Zulip):

RalfJ Does this looks like a bad idea or a terrible idea? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=72153dbf42418f6de937b4ed55dc93eb

@Simon Sapin it is certainly interesting. ;)

RalfJ (Aug 24 2019 at 11:17, on Zulip):

this is some crazy hacking around const limitations for sure

RalfJ (Aug 24 2019 at 11:17, on Zulip):

but I guess the question is more about the struct thing you are doing

RalfJ (Aug 24 2019 at 11:18, on Zulip):

I would probably prefer an array-of-MaybeUninit<u8> over a maybeuninit-array, but other than that, as much as I would like to, I cannot find anything inherently wrong with this ;)

RalfJ (Aug 24 2019 at 11:24, on Zulip):

I view unions as sugar for transmuting memory, and thats what you are implementing here. Nice hack!

DutchGhost (Aug 30 2019 at 20:26, on Zulip):

On top of this I tried implementing compiletime checked unions, keeping track of the current 'selected' type,
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1028dc58599ab7bb757d994e79e8cc4a

The test given passes Miri.

RalfJ (Aug 31 2019 at 07:01, on Zulip):

On top of this I tried implementing compiletime checked unions, keeping track of the current 'selected' type

isn't that what enum does?

Last update: Nov 19 2019 at 17:50UTC