I have a petgraph::Graph
structure onto which I have imposed a tree structure by giving every node weight a parent_edge_idx
which is an Option<EdgeIdx>
of the edge that connects from its parent to itself.
I need to iterate over a node's children. I need the edge weight of the connecting edge and the node weight of the child.
I wanted to factor that iteration into a helper function that returns a reference to an Iterator<Item = (EdgeIdx, NodeIdx)>
. I want to do this cost-free; since I have to borrow self.search_tree
in order to do this, the iterator is only valid for the lifetime of self
.
- Is this a reasonable function to want to write?
- Is it possible to write this function?
Any gated features are ok; I'm on nightly.
fn children<'a>(
&'a mut self,
node_idx: NodeIdx,
) -> &'a impl Iterator<Item = (EdgeIdx, NodeIdx)> {
&self.search_tree.neighbors(node_idx).map(|child_idx| {
let node = self.search_tree.node_weight(child_idx).unwrap();
let edge_idx = node.parent_edge_idx.unwrap();
(edge_idx, child_idx)
})
}