Skip to content

Commit

Permalink
Rollup merge of rust-lang#55421 - CAD97:patch-1, r=kennytm
Browse files Browse the repository at this point in the history
Add ManuallyDrop::take

Tracking issue: rust-lang#55422

Proposed in this form in https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679,
see that thread for some history.

A small convenience wrapper for `ManuallyDrop` that makes a pattern (taking ownership of the contained data in drop) more obvious.
  • Loading branch information
kennytm committed Oct 28, 2018
2 parents b565e5d + 0757c0f commit 360f32a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,26 @@ impl<T> ManuallyDrop<T> {
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
slot.value
}

/// Takes the contained value out.
///
/// This method is primarily intended for moving out values in drop.
/// Instead of using [`ManuallyDrop::drop`] to manually drop the value,
/// you can use this method to take the value and use it however desired.
/// `Drop` will be invoked on the returned value following normal end-of-scope rules.
///
/// If you have ownership of the container, you can use [`ManuallyDrop::into_inner`] instead.
///
/// # Safety
///
/// This function semantically moves out the contained value without preventing further usage.
/// It is up to the user of this method to ensure that this container is not used again.
#[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
#[unstable(feature = "manually_drop_take", issue = "55422")]
#[inline]
pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
ManuallyDrop::into_inner(ptr::read(slot))
}
}

impl<T: ?Sized> ManuallyDrop<T> {
Expand Down

0 comments on commit 360f32a

Please sign in to comment.