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

Topic: ManuallyDrop in MaybeUninit


est31 (Jul 08 2019 at 23:43, on Zulip):

I wonder why is there ManuallyDrop in MaybeUninit?

est31 (Jul 08 2019 at 23:44, on Zulip):

https://doc.rust-lang.org/stable/src/core/mem.rs.html#1081-1084
Why is it ManuallyDrop? I've thought that Drop is never called for unions?

est31 (Jul 08 2019 at 23:44, on Zulip):

Is the ManuallyDrop needed for anything safety related?

rkruppe (Jul 08 2019 at 23:48, on Zulip):

https://rust-lang.github.io/rfcs/2514-union-initialization-and-drop.html

est31 (Jul 08 2019 at 23:51, on Zulip):

@rkruppe so drop is called after all?

est31 (Jul 08 2019 at 23:52, on Zulip):

Oh so it's not meant for safety, but for forward compatibility?

est31 (Jul 08 2019 at 23:52, on Zulip):

For a possible future where that RFC is implemented and stabilized

est31 (Jul 08 2019 at 23:53, on Zulip):

As then e.g. MaybeUninit<Vec<u32>> would not be allowed any more?

rkruppe (Jul 08 2019 at 23:53, on Zulip):

No

rkruppe (Jul 08 2019 at 23:54, on Zulip):

The point of disallowing union fields with drop glue is so we never have to worry about what that means and can simply never call drop union fields without that being a massive surprise and footgun

est31 (Jul 08 2019 at 23:55, on Zulip):

Ok

est31 (Jul 08 2019 at 23:56, on Zulip):

but that doesn't answer my question: Is the presence of ManuallyDrop is for forwards compatibility only, to keep MaybeUninit<Vec<u32>> working even after the RFC is implemented?

est31 (Jul 08 2019 at 23:59, on Zulip):

I guess it's yes.

rkruppe (Jul 09 2019 at 00:00, on Zulip):

Oh I guess if it's not implemented yet, then it's technically for forward compatibility yes

rkruppe (Jul 09 2019 at 00:00, on Zulip):

Sorry for misunderstanding

est31 (Jul 09 2019 at 00:01, on Zulip):

Thanks, that's what I wanted to know!

RalfJ (Jul 11 2019 at 15:21, on Zulip):

ManuallyDrop is not just for compatibility

RalfJ (Jul 11 2019 at 15:21, on Zulip):

e.g. ManuallyDrop works with unsized types. And Option<ManuallyDrop<Vec<u8>>> gets layout optimizations

RalfJ (Jul 11 2019 at 15:22, on Zulip):

"I may have totally uninitialized data" vs "I want to control dropping" are not the same concerns

RalfJ (Jul 11 2019 at 15:22, on Zulip):

having uninitialized data implies you need to control dropping (hence MaybeUninit does not auto-drp), but not vice versa

Last update: Nov 19 2019 at 18:50UTC