Add conditional DerefMut for UniquePtr

This commit is contained in:
David Tolnay 2020-11-19 20:26:34 -08:00
parent da55763434
commit 300ef4a0f4
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 13 additions and 9 deletions

View File

@ -6,7 +6,7 @@ use core::ffi::c_void;
use core::fmt::{self, Debug, Display};
use core::marker::PhantomData;
use core::mem;
use core::ops::Deref;
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::ptr;
@ -119,6 +119,18 @@ where
}
}
impl<T> DerefMut for UniquePtr<T>
where
T: UniquePtrTarget + 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 UniquePtr<{}>", T::__NAME),
}
}
}
impl<T> Debug for UniquePtr<T>
where
T: Debug + UniquePtrTarget,

View File

@ -1,11 +1,3 @@
error[E0596]: cannot borrow data in a dereference of `UniquePtr<Shared>` as mutable
--> $DIR/unique_ptr_as_mut.rs:19:31
|
19 | let _: &mut ffi::Shared = &mut shared;
| ^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `UniquePtr<Shared>`
error[E0596]: cannot borrow data in a dereference of `UniquePtr<ffi::Opaque>` as mutable
--> $DIR/unique_ptr_as_mut.rs:22:31
|