I want to create Nodes (weights) which contain its own NodeIndex
struct Node {
index: NodeIndex,
...
}
However, in order to get the NodeIndex, I need to call graph.add_node(node) which requires a fully constructed Node.
Is there a way I can accomplish this without having to use Option<NodeIndex>?
I managed to change my design such that this is no longer necessary, and probably for the better.
The point of using petgraph (at least in my case) is to create a graph like structure without having to deal with self-referential types (see this post for more details).
This only works if petgraph's
Graphhas (sole?) ownership of the nodes. Once you add your node to the graph viagraph.add_node(node), it returns you theNodeIndexwhich will be your handle towards thatnodein the future (think of it as a pointer).So storing the
NodeIndexinside theNodemakes as much sense as storing the pointer to the object inside the object itself. In my case, it was a symptom of a misuse and misunderstanding of petgraph.