[librsvg] node.rs: New utility function boxed_node_new() for the node creators



commit 8aa9b069315f57102eb471d69f2675f28076a3e8
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Feb 21 20:32:56 2017 -0600

    node.rs: New utility function boxed_node_new() for the node creators
    
    Factor its code out of the rsvg_node_foo_new() from shapes.rs.

 rust/src/cnode.rs  |   11 +----------
 rust/src/node.rs   |   19 +++++++++++++++++++
 rust/src/shapes.rs |   37 +++++++++++++++----------------------
 3 files changed, 35 insertions(+), 32 deletions(-)
---
diff --git a/rust/src/cnode.rs b/rust/src/cnode.rs
index 6b8dcae..c838d09 100644
--- a/rust/src/cnode.rs
+++ b/rust/src/cnode.rs
@@ -38,15 +38,6 @@ impl Drop for CNode {
     }
 }
 
-pub fn parent_ptr_to_weak (raw_parent: *const RsvgNode) -> Option<Weak<Node>> {
-    if raw_parent.is_null () {
-        None
-    } else {
-        let p: &RsvgNode = unsafe { & *raw_parent };
-        Some (Rc::downgrade (&p.clone ()))
-    }
-}
-
 #[no_mangle]
 pub extern fn rsvg_rust_cnode_new (node_type:   NodeType,
                                    raw_parent:  *const RsvgNode,
@@ -66,7 +57,7 @@ pub extern fn rsvg_rust_cnode_new (node_type:   NodeType,
     };
 
     box_node (Rc::new (Node::new (node_type,
-                                  parent_ptr_to_weak (raw_parent),
+                                  node_ptr_to_weak (raw_parent),
                                   state,
                                   Box::new (cnode))))
 }
diff --git a/rust/src/node.rs b/rust/src/node.rs
index ef8543e..6cab60a 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -6,6 +6,7 @@ use std::cell::RefCell;
 use std::ptr;
 
 use drawing_ctx::RsvgDrawingCtx;
+use drawing_ctx;
 
 use handle::RsvgHandle;
 
@@ -144,6 +145,24 @@ impl Drop for Node {
     }
 }
 
+pub fn node_ptr_to_weak (raw_parent: *const RsvgNode) -> Option<Weak<Node>> {
+    if raw_parent.is_null () {
+        None
+    } else {
+        let p: &RsvgNode = unsafe { & *raw_parent };
+        Some (Rc::downgrade (&p.clone ()))
+    }
+}
+
+pub fn boxed_node_new (node_type:  NodeType,
+                       raw_parent: *const RsvgNode,
+                       node_impl: Box<NodeTrait>) -> *mut RsvgNode {
+    box_node (Rc::new (Node::new (node_type,
+                                  node_ptr_to_weak (raw_parent),
+                                  drawing_ctx::state_new (),
+                                  node_impl)))
+}
+
 #[no_mangle]
 pub extern fn rsvg_node_get_type (raw_node: *const RsvgNode) -> NodeType {
     assert! (!raw_node.is_null ());
diff --git a/rust/src/shapes.rs b/rust/src/shapes.rs
index a300c80..cea37a4 100644
--- a/rust/src/shapes.rs
+++ b/rust/src/shapes.rs
@@ -1,10 +1,8 @@
 use std::cell::RefCell;
 use std::ptr;
-use std::rc::Rc;
 use std::cell::Cell;
 extern crate libc;
 
-use cnode::*;
 use drawing_ctx;
 use drawing_ctx::*;
 use handle::RsvgHandle;
@@ -485,40 +483,35 @@ impl NodeTrait for NodeEllipse {
 
 #[no_mangle]
 pub extern fn rsvg_node_path_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
-    box_node (Rc::new (Node::new (NodeType::Path,
-                                  parent_ptr_to_weak (raw_parent),
-                                  drawing_ctx::state_new (),
-                                  Box::new (NodePath::new ()))))
+    boxed_node_new (NodeType::Path,
+                    raw_parent,
+                    Box::new (NodePath::new ()))
 }
 
 #[no_mangle]
 pub extern fn rsvg_node_line_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
-    box_node (Rc::new (Node::new (NodeType::Line,
-                                  parent_ptr_to_weak (raw_parent),
-                                  drawing_ctx::state_new (),
-                                  Box::new (NodeLine::new ()))))
+    boxed_node_new (NodeType::Line,
+                    raw_parent,
+                    Box::new (NodeLine::new ()))
 }
 
 #[no_mangle]
 pub extern fn rsvg_node_rect_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
-    box_node (Rc::new (Node::new (NodeType::Rect,
-                                  parent_ptr_to_weak (raw_parent),
-                                  drawing_ctx::state_new (),
-                                  Box::new (NodeRect::new ()))))
+    boxed_node_new (NodeType::Rect,
+                    raw_parent,
+                    Box::new (NodeRect::new ()))
 }
 
 #[no_mangle]
 pub extern fn rsvg_node_circle_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
-    box_node (Rc::new (Node::new (NodeType::Circle,
-                                  parent_ptr_to_weak (raw_parent),
-                                  drawing_ctx::state_new (),
-                                  Box::new (NodeCircle::new ()))))
+    boxed_node_new (NodeType::Circle,
+                    raw_parent,
+                    Box::new (NodeCircle::new ()))
 }
 
 #[no_mangle]
 pub extern fn rsvg_node_ellipse_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode 
{
-    box_node (Rc::new (Node::new (NodeType::Ellipse,
-                                  parent_ptr_to_weak (raw_parent),
-                                  drawing_ctx::state_new (),
-                                  Box::new (NodeEllipse::new ()))))
+    boxed_node_new (NodeType::Ellipse,
+                    raw_parent,
+                    Box::new (NodeEllipse::new ()))
 }


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