diff options
| author | Christopher Durham <cad97@cad97.com> | 2018-10-27 15:05:13 -0400 |
|---|---|---|
| committer | cad97 <cad97@cad97.com> | 2018-10-28 04:16:58 -0400 |
| commit | 0757c0fa7f37e1984492f4dce14b46dd686c67a9 (patch) | |
| tree | 500b987c0649bf2e4d518a0d9bc0eb71cc04a0e1 /src/libcore | |
| parent | f32f1113c93fe84f252293ae4af86be1ceae4a84 (diff) | |
| download | rust-0757c0fa7f37e1984492f4dce14b46dd686c67a9.tar.gz rust-0757c0fa7f37e1984492f4dce14b46dd686c67a9.zip | |
Add ManuallyDrop::take
https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/mem.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 27ee9556bd0..8e218fa5a46 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -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> { |
