[librsvg: 3/5] dpi: remove interior mutability



commit 6d4167470cb954a348b9043aa0d0075b82857ef9
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 11 23:20:12 2019 +0100

    dpi: remove interior mutability
    
    Make Dpi a copy type and keep interior mutability in the Handle

 rsvg_internals/src/dpi.rs       | 28 +++++++++++-----------------
 rsvg_internals/src/handle.rs    | 16 ++++++++--------
 rsvg_internals/src/structure.rs | 13 +++++--------
 3 files changed, 24 insertions(+), 33 deletions(-)
---
diff --git a/rsvg_internals/src/dpi.rs b/rsvg_internals/src/dpi.rs
index fcbe13cb..f5dec06f 100644
--- a/rsvg_internals/src/dpi.rs
+++ b/rsvg_internals/src/dpi.rs
@@ -1,5 +1,3 @@
-use std::cell::Cell;
-
 // This is configurable at runtime
 const DEFAULT_DPI_X: f64 = 90.0;
 const DEFAULT_DPI_Y: f64 = 90.0;
@@ -7,36 +5,32 @@ const DEFAULT_DPI_Y: f64 = 90.0;
 static mut DPI_X: f64 = DEFAULT_DPI_X;
 static mut DPI_Y: f64 = DEFAULT_DPI_Y;
 
-#[derive(Debug, Clone, Default)]
+#[derive(Debug, Copy, Clone, Default)]
 pub struct Dpi {
-    x: Cell<f64>,
-    y: Cell<f64>,
+    x: f64,
+    y: f64,
 }
 
 impl Dpi {
+    pub fn new(x: f64, y: f64) -> Dpi {
+        Dpi { x, y }
+    }
+
     pub fn x(&self) -> f64 {
-        if self.x.get() <= 0.0 {
+        if self.x <= 0.0 {
             unsafe { DPI_X }
         } else {
-            self.x.get()
+            self.x
         }
     }
 
-    pub fn set_x(&self, dpi_x: f64) {
-        self.x.set(dpi_x)
-    }
-
     pub fn y(&self) -> f64 {
-        if self.y.get() <= 0.0 {
+        if self.y <= 0.0 {
             unsafe { DPI_Y }
         } else {
-            self.y.get()
+            self.y
         }
     }
-
-    pub fn set_y(&self, dpi_y: f64) {
-        self.y.set(dpi_y)
-    }
 }
 
 #[no_mangle]
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index bb29511b..d9e1ca75 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -140,7 +140,7 @@ impl Drop for SizeCallback {
 }
 
 pub struct Handle {
-    dpi: Dpi,
+    dpi: Cell<Dpi>,
     base_url: RefCell<Option<Url>>,
     base_url_cstring: RefCell<Option<CString>>, // needed because the C api returns *const char
     svg: RefCell<Option<Rc<Svg>>>,
@@ -155,7 +155,7 @@ pub struct Handle {
 impl Handle {
     fn new() -> Handle {
         Handle {
-            dpi: Dpi::default(),
+            dpi: Cell::new(Dpi::default()),
             base_url: RefCell::new(None),
             base_url_cstring: RefCell::new(None),
             svg: RefCell::new(None),
@@ -309,7 +309,7 @@ impl Handle {
             f64::from(dimensions.height),
             dimensions.em,
             dimensions.ex,
-            self.dpi.clone(),
+            self.dpi.get(),
             self.is_testing.get(),
         );
 
@@ -439,7 +439,7 @@ impl Handle {
 
         if is_root {
             if let Some((root_width, root_height)) =
-                node.with_impl(|svg: &NodeSvg| svg.get_size(&self.dpi))
+                node.with_impl(|svg: &NodeSvg| svg.get_size(self.dpi.get()))
             {
                 let ink_r = RsvgRectangle {
                     x: 0.0,
@@ -821,28 +821,28 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_base_url(
 pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_x(raw_handle: *const Handle, dpi_x: f64) {
     let handle = &*(raw_handle as *const Handle);
 
-    handle.dpi.set_x(dpi_x);
+    handle.dpi.set(Dpi::new(dpi_x, handle.dpi.get().y()));
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_x(raw_handle: *const Handle) -> f64 {
     let handle = &*(raw_handle as *const Handle);
 
-    handle.dpi.x()
+    handle.dpi.get().x()
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_y(raw_handle: *const Handle, dpi_y: f64) {
     let handle = &*(raw_handle as *const Handle);
 
-    handle.dpi.set_y(dpi_y);
+    handle.dpi.set(Dpi::new(handle.dpi.get().x(), dpi_y));
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_y(raw_handle: *const Handle) -> f64 {
     let handle = &*(raw_handle as *const Handle);
 
-    handle.dpi.y()
+    handle.dpi.get().y()
 }
 
 #[no_mangle]
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index a50ead3f..a9044d5b 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -125,19 +125,16 @@ impl NodeSvg {
         }
     }
 
-    pub fn get_size(&self, dpi: &Dpi) -> Option<(i32, i32)> {
-        let dpi_x = dpi.x();
-        let dpi_y = dpi.y();
-
+    pub fn get_size(&self, dpi: Dpi) -> Option<(i32, i32)> {
         match (self.w.get(), self.h.get(), self.vbox.get()) {
             (w, h, Some(vb)) => Some((
-                w.hand_normalize(dpi_x, vb.0.width, 12.0).round() as i32,
-                h.hand_normalize(dpi_y, vb.0.height, 12.0).round() as i32,
+                w.hand_normalize(dpi.x(), vb.0.width, 12.0).round() as i32,
+                h.hand_normalize(dpi.y(), vb.0.height, 12.0).round() as i32,
             )),
             (w, h, None) if w.unit != LengthUnit::Percent && h.unit != LengthUnit::Percent => {
                 Some((
-                    w.hand_normalize(dpi_x, 0.0, 12.0).round() as i32,
-                    h.hand_normalize(dpi_y, 0.0, 12.0).round() as i32,
+                    w.hand_normalize(dpi.x(), 0.0, 12.0).round() as i32,
+                    h.hand_normalize(dpi.y(), 0.0, 12.0).round() as i32,
                 ))
             }
             (_, _, _) => None,


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