TLDR, Rc::Weak = Box :: ???
Longer Version:
Here is a concrete class:
pub enum SExp {
Num(i64),
List(Vector<Container<SExp>>), // What do we use for Container ?
}
I have the following weird requirement:
-
I want the compiler to enforce that this particular data structure is a TREE, not merely a DAG. In particular, I want to statically enforce that nodes can NOT be shared. I.e. each node has at most 1 parent.
-
I want to be able to refer to nodes and store aux data. For example, to have a HashMap that allows me to lookup the parent of a Node.
-
I want to avoid memory leaks.
===
Now, let's work through the design space a bit:
Suppose all we care about is (1). Then the right choice is to use Box instead of Rc. This ensures that we are storing a TREE, not a DAG.
Now, ignore (1) for now. focus only on (2) and (3). One way to do this is to store everything via Rc. Then, for (2), we just use Weak's to store things like "Weak_X is parent of Weak_Y".
===
So now there is where I'm stuck. As far as I know, there isn't any "???" that satisfies "Rc::Weak = Box::???". Yet, on the other hand, I want a compiler enforced "this is a TREE, not a DAG" property that Box offers over Rc.
Suggestions ?