Reduce borrow scope (#6359)

Reduce the scope of a borrow, to prevent `BorrowError`s.
This commit is contained in:
Kaz Wesley 2023-04-19 13:33:46 -07:00 committed by GitHub
parent fe0c8fd9a3
commit dd4dce2c3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -805,7 +805,12 @@ impl LayerModel {
/// Clear the parent field and remove the layer from its parent's sublayer list.
fn remove_from_parent(&self) {
if let Some(parent) = self.parent.borrow_mut().take() {
// Borrow `self.parent` only within the scope of this line. Note that if this expression
// were used directly as the matched expression of the `if let`, the `RefMut` would not be
// dropped until the end of the `if let` block, even though it is a temporary value within
// the subexpression with `take`.
let parent = self.parent.borrow_mut().take();
if let Some(parent) = parent {
parent.borrow_mut().remove(self.id());
// Recompute depth order, in case removing a parent resolved a cycle.
self.depth_order_dirty.set();