diff options
| author | kennytm <kennytm@gmail.com> | 2018-10-28 21:32:03 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-10-28 21:32:03 +0800 |
| commit | 360f32abf49802e7247e562a350d07695b322818 (patch) | |
| tree | 2c8f78edcab37272b648366d18e0db7d3bd26ee3 | |
| parent | b565e5d5752b995e878b7459ecea081d3e3b0dbd (diff) | |
| parent | 0757c0fa7f37e1984492f4dce14b46dd686c67a9 (diff) | |
| download | rust-360f32abf49802e7247e562a350d07695b322818.tar.gz rust-360f32abf49802e7247e562a350d07695b322818.zip | |
Rollup merge of #55421 - CAD97:patch-1, r=kennytm
Add ManuallyDrop::take Tracking issue: #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.
| -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 a955e0e662a..22016e8cf41 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> { |
