Quellcode durchsuchen

make get node parent mut optionally return a parent

Evan Almloff vor 2 Jahren
Ursprung
Commit
e08a2186a6
2 geänderte Dateien mit 20 neuen und 3 gelöschten Zeilen
  1. 1 1
      packages/native-core/src/real_dom.rs
  2. 19 2
      packages/native-core/src/tree.rs

+ 1 - 1
packages/native-core/src/real_dom.rs

@@ -97,7 +97,7 @@ impl<S: State> RealDom<S> {
     }
 
     fn resolve_height(&mut self, node_id: RealNodeId) {
-        if let Some((node, parent)) = self.tree.node_parent_mut(node_id) {
+        if let Some((node, Some(parent))) = self.tree.node_parent_mut(node_id) {
             let height = parent.node_data.height;
             node.node_data.height = height + 1;
             unsafe {

+ 19 - 2
packages/native-core/src/tree.rs

@@ -94,13 +94,13 @@ pub trait TreeView<T>: Sized {
 
     fn parent_mut(&mut self, id: NodeId) -> Option<&mut T>;
 
-    fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, &mut T)> {
+    fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, Option<&mut T>)> {
         let mut_ptr: *mut Self = self;
         unsafe {
             // Safety: No node has itself as a parent.
             (*mut_ptr)
                 .get_mut(id)
-                .and_then(|parent| (*mut_ptr).parent_mut(id).map(|children| (parent, children)))
+                .map(|node| (node, (*mut_ptr).parent_mut(id).map(|parent| parent)))
         }
     }
 
@@ -819,3 +819,20 @@ fn traverse_breadth_first() {
         node_count += 1;
     });
 }
+
+trait UpwardPass<T> {
+    fn upward_pass(&mut self, node: &mut T, parent: Option<&mut T>) -> bool;
+
+    fn resolve_pass(&mut self, tree: &mut impl TreeView<T>, starting_nodes: &[NodeId]) {
+        let mut stack = Vec::new();
+        for node in starting_nodes {
+            stack.push(*node);
+        }
+        while let Some(node_id) = stack.pop() {
+            let (node, parent) = tree.node_parent_mut(node_id).unwrap();
+            if self.upward_pass(node, parent) {
+                stack.push(tree.parent_id(node_id).unwrap());
+            }
+        }
+    }
+}