@@ -423,7 +423,7 @@ impl<T> Tree<T> {
423
423
fn remove_node_drop_children ( & mut self , node_id : NodeId ) -> Result < Node < T > , NodeIdError > {
424
424
let mut children = self . get_mut_unsafe ( & node_id) . take_children ( ) ;
425
425
for child in children. drain ( ..) {
426
- try! ( self . remove_node_drop_children ( child) ) ;
426
+ self . remove_node_drop_children ( child) ? ;
427
427
}
428
428
Ok ( self . remove_node_internal ( node_id) )
429
429
}
@@ -547,7 +547,7 @@ impl<T> Tree<T> {
547
547
self . root = Some ( node_id. clone ( ) ) ;
548
548
549
549
if let Some ( old_root) = old_root {
550
- try! ( self . move_node_to_parent ( & old_root, node_id) ) ;
550
+ self . move_node_to_parent ( & old_root, node_id) ? ;
551
551
}
552
552
553
553
Ok ( ( ) )
@@ -696,7 +696,8 @@ impl<T> Tree<T> {
696
696
}
697
697
698
698
///
699
- /// Moves the node to a position amongst sibling nodes.
699
+ /// Moves the node to a position amongst sibling nodes. If the `pos` provided is too large then
700
+ /// the node in question will be placed after all of its other siblings.
700
701
///
701
702
/// Any children will remain attached to this node.
702
703
///
@@ -714,44 +715,51 @@ impl<T> Tree<T> {
714
715
/// let _c3 = my_tree.insert(Node::new(3), UnderNode(&root_id)).unwrap();
715
716
/// let _c4 = my_tree.insert(Node::new(4), UnderNode(&root_id)).unwrap();
716
717
///
717
- /// for (i,n) in my_tree.children(&root_id).unwrap().enumerate() {
718
- /// println!("i={} n={:?}", i, n.data());
718
+ /// let expected_values = [1, 2, 3, 4];
719
+ /// for (child, expected) in my_tree.children(&root_id).unwrap().zip(expected_values.iter()) {
720
+ /// assert_eq!(child.data(), expected);
719
721
/// }
720
722
///
721
723
/// my_tree.make_nth_sibling(&c1, 3).unwrap();
722
724
///
723
- /// for (i,n) in my_tree.children(&root_id).unwrap().enumerate() {
724
- /// println!("i={} n={:?}", i, n.data());
725
+ /// let expected_values = [2, 3, 4, 1];
726
+ /// for (child, expected) in my_tree.children(&root_id).unwrap().zip(expected_values.iter()) {
727
+ /// assert_eq!(child.data(), expected);
725
728
/// }
726
729
/// ```
727
730
///
728
-
729
731
pub fn make_nth_sibling ( & mut self , node : & NodeId , pos : usize ) -> Result < ( ) , NodeIdError > {
730
-
731
- let parent = self . get ( node) ?. parent ( )
732
+ let parent = self
733
+ . get ( node) ?
734
+ . parent ( )
732
735
. ok_or ( NodeIdError :: NodeIdNoLongerValid ) ?
733
736
. clone ( ) ;
734
737
735
738
let num_children = self . children_ids ( & parent) ?. count ( ) ;
736
739
let pos = pos. min ( num_children - 1 ) ;
737
-
740
+
738
741
// First determine the current index that the node has
739
742
// unwrap should not be reachable, since we are searching under node's
740
743
// own parent, barring bugs in id_tree
741
- let mut current_pos = self . children_ids ( & parent) ?
744
+ let mut current_pos = self
745
+ . children_ids ( & parent) ?
742
746
. enumerate ( )
743
- . find_map ( |( i, n) | if n== node { Some ( i) } else { None } )
747
+ . find_map ( |( i, n) | if n == node { Some ( i) } else { None } )
744
748
. unwrap ( ) ;
745
-
749
+
746
750
while current_pos != pos {
747
751
let pos_to_swap = if current_pos < pos {
748
- current_pos+ 1
752
+ current_pos + 1
749
753
} else if current_pos > pos {
750
- current_pos- 1
754
+ current_pos - 1
751
755
} else {
752
756
break ;
753
757
} ;
754
- let node_to_swap = self . children_ids ( & parent) ?. nth ( pos_to_swap) . unwrap ( ) . clone ( ) ;
758
+ let node_to_swap = self
759
+ . children_ids ( & parent) ?
760
+ . nth ( pos_to_swap)
761
+ . unwrap ( )
762
+ . clone ( ) ;
755
763
self . swap_nodes ( node, & node_to_swap, SwapBehavior :: TakeChildren ) ?;
756
764
current_pos = pos_to_swap;
757
765
}
0 commit comments