From ac186354354e6032c34ad95ce22d526371729cb6 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sun, 21 Oct 2018 23:54:35 +0200 Subject: [PATCH] Add MaybeUninit::new Sometimes it *is* initialized! --- src/libcore/lib.rs | 1 + src/libcore/mem.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 59cc312bee5fc..1eded42683073 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -82,6 +82,7 @@ #![feature(const_fn)] #![feature(const_int_ops)] #![feature(const_fn_union)] +#![feature(const_manually_drop_new)] #![feature(custom_attribute)] #![feature(doc_cfg)] #![feature(doc_spotlight)] diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 27ee9556bd089..a955e0e662a6c 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1021,6 +1021,15 @@ pub union MaybeUninit { } impl MaybeUninit { + /// Create a new `MaybeUninit` initialized with the given value. + /// + /// Note that dropping a `MaybeUninit` will never call `T`'s drop code. + /// It is your responsibility to make sure `T` gets dropped if it got initialized. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub const fn new(val: T) -> MaybeUninit { + MaybeUninit { value: ManuallyDrop::new(val) } + } + /// Create a new `MaybeUninit` in an uninitialized state. /// /// Note that dropping a `MaybeUninit` will never call `T`'s drop code.