Trait cactusref::Adopt

source ·
pub unsafe trait Adopt: Sealed {
    // Required methods
    unsafe fn adopt_unchecked(this: &Self, other: &Self);
    fn unadopt(this: &Self, other: &Self);
}
Expand description

Build a graph of linked Rc smart pointers to enable busting cycles on drop.

Calling adopt_unchecked builds an object graph which can be used by to detect cycles.

§Safety

Implementors of this trait must ensure that bookkeeping edges in the object graph is correct because these links are used to determine whether an Rc is reachable in Rc’s Drop implementation. Failure to properly bookkeep the object graph will result in undefined behavior.

Undefined behavior may include:

  • Memory leaks.
  • Double-frees.
  • Dangling Rcs which will cause a use after free.

Required Methods§

source

unsafe fn adopt_unchecked(this: &Self, other: &Self)

Perform bookkeeping to record that this has an owned reference to other.

Adoption is a one-way link, or a directed edge in the object graph which means “this owns other”.

adopt can be called multiple times for a pair of Rcs. Each call to adopt indicates that this owns one distinct clone of other.

This is an associated function that needs to be used as Adopt::adopt_unchecked(...). A method would interfere with methods of the same name on the contents of a Rc used through Deref.

§Safety

Callers must ensure that this owns a strong reference to other.

Callers should call unadopt when this no longer holds a strong reference to other to avoid memory leaks, but this is not required for soundness.

source

fn unadopt(this: &Self, other: &Self)

Perform bookkeeping to record that this has removed an owned reference to other.

Adoption is a one-way link, or a directed edge in the object graph which means “this owns other”.

This is an associated function that needs to be used as Adopt::unadopt(...). A method would interfere with methods of the same name on the contents of a Rc used through Deref.

§Memory Leaks

Failure to call this function when removing an owned Rc from this is safe, but may result in a memory leak.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> Adopt for Rc<T>

Implementation of Adopt for Rc which enables Rcs to form a cycle of strong references that are reaped by Rc’s Drop implementation.