Remove SharedPtr to &mut conversions

These are broken because multiple SharedPtrs can point to the same data,
and getting a &mut from each of them would result in multiple &mut to
overlapping data.
This commit is contained in:
David Tolnay 2020-12-10 20:06:56 -08:00
parent 1cde514d94
commit 9df2aac134
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -4,8 +4,7 @@ use core::ffi::c_void;
use core::fmt::{self, Debug, Display};
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::ops::Deref;
/// BInding to C++ `std::shared_ptr<T>`.
#[repr(C)]
@ -61,29 +60,6 @@ where
let this = self as *const Self as *const c_void;
unsafe { T::__get(this).as_ref() }
}
/// Returns a mutable pinned reference to the object owned by this SharedPtr
/// if any, otherwise None.
pub fn as_mut(&mut self) -> Option<Pin<&mut T>> {
let this = self as *mut Self as *mut c_void;
unsafe {
let mut_reference = (T::__get(this) as *mut T).as_mut()?;
Some(Pin::new_unchecked(mut_reference))
}
}
/// Returns a mutable pinned reference to the object owned by this
/// SharedPtr.
///
/// # Panics
///
/// Panics if the SharedPtr holds a null pointer.
pub fn pin_mut(&mut self) -> Pin<&mut T> {
match self.as_mut() {
Some(target) => target,
None => panic!("called pin_mut on a null SharedPtr<{}>", T::__NAME),
}
}
}
unsafe impl<T> Send for SharedPtr<T> where T: Send + Sync + SharedPtrTarget {}
@ -128,18 +104,6 @@ where
}
}
impl<T> DerefMut for SharedPtr<T>
where
T: SharedPtrTarget + Unpin,
{
fn deref_mut(&mut self) -> &mut Self::Target {
match self.as_mut() {
Some(target) => Pin::into_inner(target),
None => panic!("called deref_mut on a null SharedPtr<{}>", T::__NAME),
}
}
}
impl<T> Debug for SharedPtr<T>
where
T: Debug + SharedPtrTarget,