I have a Tree structure defined with the following data structures.
the root nodes are stored into a Vector.
#[derive(Debug)]
pub struct Node {
pub object: BmosHaystackObject,
pub parent: Option<NodeRefNodeRefWeak>,
pub children: Vec<NodeRef>,
}
pub type NodeRef = Rc<RefCell<Node>>;
pub type NodeRefNodeRefWeak = Weak<RefCell<Node>>;
root nodes: Vec<NodeRef>
I can build a tree using this structure and print out to json but I am having trouble doing some basic tree operations for example removing a node and its children.
I have the following method
fn tree_remove_node_children(node: &mut NodeRef) {
let mut borrow = node.borrow_mut();
let node_id: String = borrow.object.id().to_string();
for n in borrow.children.iter_mut() {
BmosConnection::tree_remove_node_children(n);
}
drop(borrow);
let parent = node.borrow().parent.clone().expect("expected parent");
let mut p = parent.upgrade().expect("expected upgrade");
let mut children = &mut p.borrow_mut().children;
children.retain(|n| n.borrow().object.id() != node_id );
}
Here I get panicked at 'already borrowed: BorrowMutError'
at the line
let mut children = &mut p.borrow_mut().children;
I am not sure how it is borrowed at this point ?
It there a better way to handle this ?
Note I have not tested whther the function would work as intended as I can't get past the borrow error at the moment.
Thanks