Skip to content

Commit 360f32a

Browse files
committed
Rollup merge of rust-lang#55421 - CAD97:patch-1, r=kennytm
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.
2 parents b565e5d + 0757c0f commit 360f32a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: src/libcore/mem.rs

+20
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,26 @@ impl<T> ManuallyDrop<T> {
973973
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
974974
slot.value
975975
}
976+
977+
/// Takes the contained value out.
978+
///
979+
/// This method is primarily intended for moving out values in drop.
980+
/// Instead of using [`ManuallyDrop::drop`] to manually drop the value,
981+
/// you can use this method to take the value and use it however desired.
982+
/// `Drop` will be invoked on the returned value following normal end-of-scope rules.
983+
///
984+
/// If you have ownership of the container, you can use [`ManuallyDrop::into_inner`] instead.
985+
///
986+
/// # Safety
987+
///
988+
/// This function semantically moves out the contained value without preventing further usage.
989+
/// It is up to the user of this method to ensure that this container is not used again.
990+
#[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
991+
#[unstable(feature = "manually_drop_take", issue = "55422")]
992+
#[inline]
993+
pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
994+
ManuallyDrop::into_inner(ptr::read(slot))
995+
}
976996
}
977997

978998
impl<T: ?Sized> ManuallyDrop<T> {

0 commit comments

Comments
 (0)