From f288bcb53caeb847b7fea5c34f83dccb589538ff Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 30 Sep 2021 17:02:06 -0500 Subject: [PATCH 1/2] 1. [feature] add parent_mut method to Node --- src/node.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/node.rs b/src/node.rs index 386f794..47f85ad 100644 --- a/src/node.rs +++ b/src/node.rs @@ -189,6 +189,24 @@ impl Node { None } + /// Returns a mutable reference to the parent node of this node, + /// or None if it is the root node. + /// Also see the `parent` method. + pub fn parent_mut( &self ) -> Option<&mut Node> { + let mut node = self.non_null(); + unsafe { + while let Some( parent ) = node.as_ref().up { + if parent.as_ref().is_forest() { + node = parent; + } else { + return Some( &mut *parent.as_ptr() ); + } + } + } + None + } + + /// Inserts sib tree before `self`. /// The newly inserted node will not be iterated over by the currently running iterator. /// From f5be99c47c63f8a559e6d7c9aa4c19aa3d9c3d2c Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sat, 2 Oct 2021 22:14:52 -0500 Subject: [PATCH 2/2] 1. [fix] mark parent_mut as unsafe fn --- src/node.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/node.rs b/src/node.rs index 47f85ad..063bd45 100644 --- a/src/node.rs +++ b/src/node.rs @@ -190,17 +190,15 @@ impl Node { } /// Returns a mutable reference to the parent node of this node, - /// or None if it is the root node. + /// or None if it is the root node. Ensure that there is only one reference to the returned value. /// Also see the `parent` method. - pub fn parent_mut( &self ) -> Option<&mut Node> { + pub unsafe fn parent_mut( &self ) -> Option<&mut Node> { let mut node = self.non_null(); - unsafe { - while let Some( parent ) = node.as_ref().up { - if parent.as_ref().is_forest() { - node = parent; - } else { - return Some( &mut *parent.as_ptr() ); - } + while let Some( parent ) = node.as_ref().up { + if parent.as_ref().is_forest() { + node = parent; + } else { + return Some( &mut *parent.as_ptr() ); } } None