borrow: update to ManuallyDrop::take

and fixup into_boxed_inner along the way

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-10-13 10:39:11 +02:00
parent d18292192d
commit 087bf31567

View File

@ -45,9 +45,9 @@ use std::mem::ManuallyDrop;
/// usage.tied.i_am_a_borrow(); /// usage.tied.i_am_a_borrow();
/// ``` /// ```
pub struct Tied<T, U: ?Sized> { pub struct Tied<T, U: ?Sized> {
// 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. /// The contained "value" of which we want to borrow something.
inner: Option<Box<T>>, inner: ManuallyDrop<Box<T>>,
/// The thing borrowing from `inner`. This is what the `Tied` value ultimately dereferences to. /// The thing borrowing from `inner`. This is what the `Tied` value ultimately dereferences to.
borrow: ManuallyDrop<Box<U>>, borrow: ManuallyDrop<Box<U>>,
} }
@ -57,8 +57,7 @@ impl<T, U: ?Sized> Drop for Tied<T, U> {
unsafe { unsafe {
// let's be explicit about order here! // let's be explicit about order here!
ManuallyDrop::drop(&mut self.borrow); ManuallyDrop::drop(&mut self.borrow);
let _ = self.inner.take(); ManuallyDrop::drop(&mut self.inner);
//ManuallyDrop::drop(&mut self.inner);
} }
} }
} }
@ -74,17 +73,18 @@ impl<T, U: ?Sized> Tied<T, U> {
let mut value = Box::new(value); let mut value = Box::new(value);
let borrow = producer(&mut *value); let borrow = producer(&mut *value);
Self { Self {
inner: Some(value), inner: ManuallyDrop::new(value),
borrow: ManuallyDrop::new(borrow), borrow: ManuallyDrop::new(borrow),
} }
} }
pub fn into_boxed_inner(mut self) -> Box<T> { pub fn into_boxed_inner(mut self) -> Box<T> {
unsafe { let inner = unsafe {
ManuallyDrop::drop(&mut self.borrow); ManuallyDrop::drop(&mut self.borrow);
//ManuallyDrop::take(&mut self.inner) ManuallyDrop::take(&mut self.inner)
} };
self.inner.take().unwrap() std::mem::forget(self);
inner
} }
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {