I have a wide tree that's represented as a Slab<{ children: ArrayVec<[{ index: usize, bound: B }; 32]> }>
. I want to do a DFS on it but it's really annoying because I'm trying to mutably borrow the array to get to the children, while I'm also mutably borrowing the array to get to the parent. How can I convince the compiler that it's all good?
fn visit_branch<B, D, I, F>(
tree: &mut RTree<B, D, I>,
visit_leaf: &F,
branch_id: I,
reinsert: &mut Vec<(I, B)>,
) -> Update<B>
where
B: Bound,
I: ID,
F: Fn(I, &D, &B) -> Update<B>,
{
let mut dirty = false;
let branch = &mut tree.branches[branch_id.into_usize()];
//TODO: How to get your laundry eaten.
let branch: *mut _ = branch;
let branch = unsafe { &mut *branch };
if branch.end {
let leaves = &mut tree.leaves;
branch.children.retain(|(id, bound)| {
match visit_leaf(*id, &leaves[id.into_usize()], bound) {
Update::Keep => true,
Update::Remove => {
leaves.remove(id.into_usize());
dirty = true;
false
}
Update::Change(b) => {
reinsert.push((*id, b));
dirty = true;
false
}
}
});
} else {
branch.children.retain(|(id, bound)| {
match visit_branch(tree, visit_leaf, *id, reinsert) {
Update::Keep => true,
Update::Remove => {
tree.branches.remove(id.into_usize());
dirty = true;
false
}
Update::Change(b) => {
*bound = b;
dirty = true;
true
}
}
});
}
if dirty {
let mut bounds = branch.children.iter().map(|(_, bound)| bound);
if let Some(&new_bound) = bounds.next() {
Update::Change(bounds.fold(new_bound, |x, y| x.merge(y)))
} else {
Update::Remove
}
} else {
Update::Keep
}
}