[librsvg] Node::foreach_child() / Node::has_children(): new functions.



commit b08c30eece1f328d69d4ea4ee7eae418d84a3917
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Dec 19 15:36:27 2017 -0600

    Node::foreach_child() / Node::has_children(): new functions.
    
    Make the Node.children field private.

 rust/src/gradient.rs  | 10 ++++++----
 rust/src/node.rs      | 20 ++++++++++++--------
 rust/src/pattern.rs   |  3 +--
 rust/src/structure.rs |  8 +++++---
 4 files changed, 24 insertions(+), 17 deletions(-)
---
diff --git a/rust/src/gradient.rs b/rust/src/gradient.rs
index c52a07c..6cf7228 100644
--- a/rust/src/gradient.rs
+++ b/rust/src/gradient.rs
@@ -249,19 +249,21 @@ impl Gradient {
     fn add_color_stops_from_node (&mut self, node: &RsvgNode) {
         assert! (node.get_type () == NodeType::LinearGradient || node.get_type () == 
NodeType::RadialGradient);
 
-        for child in &*node.children.borrow () {
+        node.foreach_child(|child| {
             if child.get_type () != NodeType::Stop {
-                continue; // just ignore this child; we are only interested in gradient stops
+                return true; // just ignore this child; we are only interested in gradient stops
             }
 
             if child.get_result ().is_err () {
-                break; // don't add any more stops
+                return false; // don't add any more stops
             }
 
             child.with_impl (|stop: &NodeStop| {
                 self.add_color_stop (stop.get_offset (), stop.get_rgba ());
             });
-        }
+
+            true
+        });
     }
 
     fn add_color_stop (&mut self, offset: f64, rgba: u32) {
diff --git a/rust/src/node.rs b/rust/src/node.rs
index 38cbeff..2aadfd9 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -57,12 +57,12 @@ impl_downcast! (NodeTrait);
 pub type NodeResult = Result<(), NodeError>;
 
 pub struct Node {
-    node_type:     NodeType,
-    parent:        Option<Weak<Node>>,       // optional; weak ref to parent
-    pub children:  RefCell<Vec<Rc<Node>>>,   // strong references to children
-    state:         *mut RsvgState,
-    result:        RefCell <NodeResult>,
-    node_impl:     Box<NodeTrait>
+    node_type: NodeType,
+    parent:    Option<Weak<Node>>,       // optional; weak ref to parent
+    children:  RefCell<Vec<Rc<Node>>>,   // strong references to children
+    state:     *mut RsvgState,
+    result:    RefCell <NodeResult>,
+    node_impl: Box<NodeTrait>
 }
 
 /* Keep this in sync with rsvg-private.h:RsvgNodeType */
@@ -222,8 +222,8 @@ impl Node {
         }
     }
 
-    pub fn foreach_child<F>(&self, f: F)
-        where F: Fn(Rc<Node>) -> bool
+    pub fn foreach_child<F>(&self, mut f: F)
+        where F: FnMut(Rc<Node>) -> bool
     {
         for c in &*self.children.borrow() {
             let next = f(c.clone());
@@ -232,6 +232,10 @@ impl Node {
             }
         }
     }
+
+    pub fn has_children(&self) -> bool {
+        self.children.borrow().len() > 0
+    }
 }
 
 // Sigh, rsvg_state_free() is only available if we are being linked into
diff --git a/rust/src/pattern.rs b/rust/src/pattern.rs
index 5822ee6..19e6333 100644
--- a/rust/src/pattern.rs
+++ b/rust/src/pattern.rs
@@ -70,8 +70,7 @@ fn node_has_children (node: &Option<Weak<Node>>) -> bool {
 
         Some (ref weak) => {
             let ref strong_node = weak.clone ().upgrade ().unwrap ();
-            let has_children = strong_node.children.borrow ().len () > 0;
-            has_children
+            strong_node.has_children()
         }
     }
 }
diff --git a/rust/src/structure.rs b/rust/src/structure.rs
index 62f7c7f..8a0ea7c 100644
--- a/rust/src/structure.rs
+++ b/rust/src/structure.rs
@@ -88,7 +88,7 @@ impl NodeTrait for NodeSwitch {
 
         drawing_ctx::push_discrete_layer (draw_ctx);
 
-        for child in &*node.children.borrow () {
+        node.foreach_child(|child| {
             if drawing_ctx::state_get_cond_true (child.get_state ()) {
                 let boxed_child = box_node (child.clone ());
 
@@ -96,9 +96,11 @@ impl NodeTrait for NodeSwitch {
 
                 rsvg_node_unref (boxed_child);
 
-                break;
+                false // just draw this child
+            } else {
+                true
             }
-        }
+        });
 
         drawing_ctx::pop_discrete_layer (draw_ctx);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]