[librsvg: 1/13] Handle: store a Document, not an Rc<Document>



commit 577cf971827051290ff832f302d06b6f519dfd79
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jan 16 19:08:45 2020 -0600

    Handle: store a Document, not an Rc<Document>
    
    We needed the Rc because both DrawingCtx and AcquiredNodes want a
    reference to the Document, but this can just be a plain reference
    instead of a refcount.
    
    This will let us have a &mut Handle, with its mutable document, to
    change the user's stylesheet.

 rsvg_internals/src/drawing_ctx.rs | 22 +++++++++++-----------
 rsvg_internals/src/handle.rs      | 15 +++++++--------
 2 files changed, 18 insertions(+), 19 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 6e7d622d..46dfe4ef 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -84,8 +84,8 @@ pub enum ClipMode {
     ClipToVbox,
 }
 
-pub struct DrawingCtx {
-    document: Rc<Document>,
+pub struct DrawingCtx<'i> {
+    document: &'i Document,
 
     initial_affine: cairo::Matrix,
 
@@ -99,22 +99,22 @@ pub struct DrawingCtx {
 
     drawsub_stack: Vec<RsvgNode>,
 
-    acquired_nodes: AcquiredNodes,
+    acquired_nodes: AcquiredNodes<'i>,
 
     measuring: bool,
     testing: bool,
 }
 
-impl DrawingCtx {
+impl<'i> DrawingCtx<'i> {
     pub fn new(
-        document: Rc<Document>,
+        document: &'i Document,
         node: Option<&RsvgNode>,
         cr: &cairo::Context,
         viewport: Rect,
         dpi: Dpi,
         measuring: bool,
         testing: bool,
-    ) -> DrawingCtx {
+    ) -> DrawingCtx<'i> {
         let initial_affine = cr.get_matrix();
 
         // This is more or less a hack to make measuring geometries possible,
@@ -144,7 +144,7 @@ impl DrawingCtx {
         let mut view_box_stack = Vec::new();
         view_box_stack.push(vbox);
 
-        let acquired_nodes = AcquiredNodes::new(document.clone());
+        let acquired_nodes = AcquiredNodes::new(document);
 
         let mut draw_ctx = DrawingCtx {
             document,
@@ -1346,14 +1346,14 @@ impl AcquiredNode {
 /// Note that if you acquire a node, you have to release it before trying to
 /// acquire it again.  If you acquire a node "#foo" and don't release it before
 /// trying to acquire "foo" again, you will obtain a None the second time.
-struct AcquiredNodes {
-    document: Rc<Document>,
+struct AcquiredNodes<'i> {
+    document: &'i Document,
     num_elements_acquired: usize,
     node_stack: Rc<RefCell<NodeStack>>,
 }
 
-impl AcquiredNodes {
-    fn new(document: Rc<Document>) -> AcquiredNodes {
+impl<'i> AcquiredNodes<'i> {
+    fn new(document: &Document) -> AcquiredNodes {
         AcquiredNodes {
             document,
             num_elements_acquired: 0,
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index fef3ccdd..6d9e6ac1 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -4,7 +4,6 @@
 
 use std::cell::Cell;
 use std::ptr;
-use std::rc::Rc;
 
 use locale_config::{LanguageRange, Locale};
 
@@ -193,7 +192,7 @@ impl Drop for SizeCallback {
 }
 
 pub struct Handle {
-    document: Rc<Document>,
+    document: Document,
 }
 
 impl Handle {
@@ -203,11 +202,11 @@ impl Handle {
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Handle, LoadingError> {
         Ok(Handle {
-            document: Rc::new(Document::load_from_stream(
+            document: Document::load_from_stream(
                 load_options,
                 stream,
                 cancellable,
-            )?),
+            )?,
         })
     }
 
@@ -303,7 +302,7 @@ impl Handle {
         let target = cairo::ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
         let cr = cairo::Context::new(&target);
         let mut draw_ctx = DrawingCtx::new(
-            self.document.clone(),
+            &self.document,
             Some(node),
             &cr,
             viewport,
@@ -462,7 +461,7 @@ impl Handle {
 
         cr.save();
         let mut draw_ctx = DrawingCtx::new(
-            self.document.clone(),
+            &self.document,
             node.as_ref(),
             cr,
             Rect::from(*viewport),
@@ -489,7 +488,7 @@ impl Handle {
         let cr = cairo::Context::new(&target);
 
         let mut draw_ctx = DrawingCtx::new(
-            self.document.clone(),
+            &self.document,
             None,
             &cr,
             unit_rectangle(),
@@ -561,7 +560,7 @@ impl Handle {
         cr.translate(-ink_r.x0, -ink_r.y0);
 
         let mut draw_ctx = DrawingCtx::new(
-            self.document.clone(),
+            &self.document,
             None,
             &cr,
             unit_rectangle(),


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