From 087bf315674055535b33bea873952a2ee59e7060 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 13 Oct 2021 10:39:11 +0200 Subject: [PATCH] borrow: update to ManuallyDrop::take and fixup into_boxed_inner along the way Signed-off-by: Wolfgang Bumiller --- proxmox-borrow/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/proxmox-borrow/src/lib.rs b/proxmox-borrow/src/lib.rs index 2db78741..36a2e90f 100644 --- a/proxmox-borrow/src/lib.rs +++ b/proxmox-borrow/src/lib.rs @@ -45,9 +45,9 @@ use std::mem::ManuallyDrop; /// usage.tied.i_am_a_borrow(); /// ``` pub struct Tied { - // FIXME: ManuallyDrop::take() is nightly-only so we need an Option for inner for now... /// The contained "value" of which we want to borrow something. - inner: Option>, + inner: ManuallyDrop>, + /// The thing borrowing from `inner`. This is what the `Tied` value ultimately dereferences to. borrow: ManuallyDrop>, } @@ -57,8 +57,7 @@ impl Drop for Tied { unsafe { // let's be explicit about order here! ManuallyDrop::drop(&mut self.borrow); - let _ = self.inner.take(); - //ManuallyDrop::drop(&mut self.inner); + ManuallyDrop::drop(&mut self.inner); } } } @@ -74,17 +73,18 @@ impl Tied { let mut value = Box::new(value); let borrow = producer(&mut *value); Self { - inner: Some(value), + inner: ManuallyDrop::new(value), borrow: ManuallyDrop::new(borrow), } } pub fn into_boxed_inner(mut self) -> Box { - unsafe { + let inner = unsafe { ManuallyDrop::drop(&mut self.borrow); - //ManuallyDrop::take(&mut self.inner) - } - self.inner.take().unwrap() + ManuallyDrop::take(&mut self.inner) + }; + std::mem::forget(self); + inner } pub fn into_inner(self) -> T {