Skip to content

Commit

Permalink
Add ManuallyDrop::take
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Oct 28, 2018
1 parent f32f111 commit 0757c0f
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 0757c0f

Please sign in to comment.