Trait moveit::unique::DerefMove [−][src]
Moving dereference operations.
A type which implements DerefMove
is the sole owner is its pointee; that
is, if P: DerefMove
, and p: P
is in-scope, then once p
goes out of
scope, *p
will become unreachable for the remainder of the program.
For example:
Box<T>
implementsDerefMove
, almost by definition.StackBox<T>
implementsDerefMove
, again by definition.- [
&mut T
] does not implementDerefMove
, because it is necessarilly a borrow of a longer-lived, “trully owning” reference. Rc<T>
andArc<T>
do not implementDerefMove
, because even though they own their pointees, they are not the sole owners.Pin<P>
forP: DerefMove
implementsDerefMove
only whenP::Target: Unpin
, sinceDerefMove: DerefMut
.
Safety
Implementing this safe requires that the uniqueness requirement described above is upheld; in particular, the following function must not violate memory safety:
fn move_out_of<P>(mut p: P) -> P::Target where P: DerefMove, P::Target: Sized, { unsafe { // Copy the pointee out of `p`. let val = (&mut *p as *mut P::Target).read(); // Destroy `p`'s storage without running the pointee's // destructor. let ptr = &mut p as *mut P; std::mem::forget(p); P::outer_drop(ptr); // Return the moved pointee. val } }
Implementations on Foreign Types
Loading content...Implementors
Loading content...