Skip to content

Commit a94b2cb

Browse files
committed
Add safety docs about T's invariants in MaybeUninit::assume_init_drop.
1 parent a14efd1 commit a94b2cb

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

library/core/src/mem/maybe_uninit.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -580,17 +580,23 @@ impl<T> MaybeUninit<T> {
580580
///
581581
/// # Safety
582582
///
583-
/// Calling this when the content is not yet fully initialized causes undefined
584-
/// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really
585-
/// is in an initialized state.
586-
///
587-
/// This function runs the destructor of the contained value in place.
588-
/// Afterwards, the memory is considered uninitialized again, but remains unmodified.
583+
/// It is up to the caller to guarantee that the `MaybeUninit<T>` really is
584+
/// in an initialized state. Calling this when the content is not yet fully
585+
/// initialized causes undefined behavior.
586+
///
587+
/// On top of that, all additional invariants of the type `T` must be
588+
/// satisfied, as the `Drop` implementation of `T` (or its members) may
589+
/// rely on this. For example, a `1`-initialized [`Vec<T>`] is considered
590+
/// initialized (under the current implementation; this does not constitute
591+
/// a stable guarantee) because the only requirement the compiler knows
592+
/// about it is that the data pointer must be non-null. Dropping such a
593+
/// `Vec<T>` however will cause undefined behaviour.
589594
///
590595
/// [`assume_init`]: MaybeUninit::assume_init
591596
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
592597
pub unsafe fn assume_init_drop(&mut self) {
593-
// SAFETY: the caller must guarantee that `self` is initialized.
598+
// SAFETY: the caller must guarantee that `self` is initialized and
599+
// satisfies all invariants of `T`.
594600
// Dropping the value in place is safe if that is the case.
595601
unsafe { ptr::drop_in_place(self.as_mut_ptr()) }
596602
}

0 commit comments

Comments
 (0)