Trait moveit::unique::DerefMove[][src]

pub unsafe trait DerefMove: DerefMut + OuterDrop { }

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> implements DerefMove, almost by definition.
  • StackBox<T> implements DerefMove, again by definition.
  • [&mut T] does not implement DerefMove, because it is necessarilly a borrow of a longer-lived, “trully owning” reference.
  • Rc<T> and Arc<T> do not implement DerefMove, because even though they own their pointees, they are not the sole owners.
  • Pin<P> for P: DerefMove implements DerefMove only when P::Target: Unpin, since DerefMove: 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

impl<T> DerefMove for Box<T>[src]

Loading content...

Implementors

impl<P: DerefMut + OuterDrop> DerefMove for Unique<P>[src]

impl<T> DerefMove for StackBox<'_, T>[src]

Loading content...