I've got infinitely-large tree which I need to traverse in specific order (like in A*). The tree has to be built lazily, and there needs to be a queue for nodes yet to be traversed, and this creates a tricky case that I don't think RefCell
can solve.
- The tree is infinitely large, so I have to create nodes lazily. I could use
RefCell
for that, but - I need to store nodes to be traversed in
BinaryHeap
queue, so lifetime ofRefCell
borrow is too short for that - Creation of nodes is very expensive, and I have to conserve memory, so I can't use solutions that clone/recreate nodes on the fly.
I've tried:
struct Node {
children: RefCell<Option<(&Node, &Node)>>;
}
impl Node {
fn traverse(&self, queue: &mut BinaryHeap) {
if nodes not created {
// Mutably create here
}
queue.push(self.children);
}
}
while let Some(node) = queue.pop() {
traverse(node, &mut queue);
}
How can I make it work?