RelRc
Reference counted pointers that may depend on other pointers. This crate replicates the behaviour of std::rc::Rc and std::rc::Weak but allows
references to link to other references. A [RelRc] object (the equivalent
of Rc) will stay alive for as long as there are references either to it or
to one of its descendants.
Can't I just keep a list of Rcs within my Rc?
Yes---and that is what this crate does under the hood. This crate however keeps track of additional data for you, thus providing additional functionalities:
- dependencies can be traversed backward: []
RelRc::all_children(v)] will return all objects (children) that depend onv(parent). - data can be stored on the dependency edges themselves.
- the resulting directed acyclic dependency graph is exposed and can be
traversed using [
petgraph] (make sure to activate thepetgraphfeature).
This crate can also be viewed as a directed acyclic graph (DAG) implementation, in which nodes are automatically removed when they and their descendants go out of scope. This is a data structure that arises naturally in use cases where new objects that are created can be viewed as descendants of previously created objects. Examples include commits in a git history, Merkle trees...
Node immutability
By design and just like [Rc], a [RelRc] and its parents are immutable once created.
New [RelRc] can be added as descendants of the node, but the node cannot
be modified once it has been created and will exist until all references to
it are dropped.
This ensures that no cycles can be created, removing the need for cyclicity checks and guaranteeing the absence of memory leaks.
Another consequence of this design decision is that all DAGs will have a single
source, from which all descending nodes can be reached.
If node or edge values need to be mutable, consider using RefCells.
Example
use RelRc;
// Create a Rc pointer (equivalent to `Rc::new`)
let root = new;
// Now child pointers can be created. Edge value: ()
let child = with_parents;
// Create a grandchild pointer
let grandchild = with_parents;
// Obtain a second reference to the child pointer
let child_clone = child.clone;
assert_eq!;
assert_eq!;
// Drop the original child node
drop;
// The second child pointer still exists: outgoing edges remain the same
assert_eq!;
assert_eq!;
// Drop the second child pointer
drop;
// Still no change at the root: grandchild node is still a descendant
assert_eq!;
// Finally, dropping the grandchild leaves only the root node
drop;
assert_eq!;