From 5eb4ca677d5c8e803857c957a60536ec759ad42d Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 1 Feb 2023 13:59:08 -0600 Subject: [PATCH] don't panic when trying to get information about a node that does not exist --- packages/native-core/src/tree.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/native-core/src/tree.rs b/packages/native-core/src/tree.rs index 9cf59dfcc..2ea71c3eb 100644 --- a/packages/native-core/src/tree.rs +++ b/packages/native-core/src/tree.rs @@ -48,6 +48,10 @@ impl Tree { self.node_slab().get(id).unwrap() } + pub fn try_get_node_data(&self, id: NodeId) -> Option<&Node> { + self.node_slab().get(id) + } + fn node_slab_mut(&mut self) -> &mut SlabStorage { self.nodes.write_slab() } @@ -189,15 +193,15 @@ impl Tree { } pub fn parent_id(&self, id: NodeId) -> Option { - self.get_node_data(id).parent + self.try_get_node_data(id).and_then(|node| node.parent) } pub fn children_ids(&self, id: NodeId) -> Option<&[NodeId]> { - Some(&self.get_node_data(id).children) + self.try_get_node_data(id).map(|node| &*node.children) } pub fn height(&self, id: NodeId) -> Option { - Some(self.get_node_data(id).height) + self.try_get_node_data(id).map(|node| node.height) } } @@ -515,7 +519,9 @@ impl<'a, T> SlabEntry<'a, T> { impl AnySlabStorageImpl for SlabStorage { fn remove(&mut self, id: NodeId) { - self.data[id.0].take(); + if let Some(entry) = self.data.get_mut(id.0) { + let _ = entry.take(); + } } fn as_any(&self) -> &dyn Any {