[librsvg: 12/14] handle: move is_loaded checks to rust



commit 4755d10472ac307d65727f1f1fa1d0a8bfae17eb
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Jan 5 23:35:44 2019 +0100

    handle: move is_loaded checks to rust

 librsvg/rsvg-handle.c        | 29 ---------------------
 rsvg_internals/src/handle.rs | 61 +++++++++++++++++++++++++++++---------------
 rsvg_internals/src/lib.rs    |  1 -
 3 files changed, 41 insertions(+), 50 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 31eb2e73..e10bdc49 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -154,7 +154,6 @@ extern guint rsvg_handle_rust_get_flags (RsvgHandleRust *raw_handle);
 extern void rsvg_handle_rust_set_flags (RsvgHandleRust *raw_handle, guint flags);
 extern guint rsvg_handle_rust_set_testing (RsvgHandleRust *raw_handle, gboolean testing);
 extern gboolean rsvg_handle_rust_is_at_start_for_setting_base_file (RsvgHandle *handle);
-extern gboolean rsvg_handle_rust_is_loaded (RsvgHandle *handle);
 extern gboolean rsvg_handle_rust_read_stream_sync (RsvgHandle *handle,
                                                    GInputStream *stream,
                                                    GCancellable *cancellable,
@@ -990,10 +989,6 @@ rsvg_handle_render_cairo_sub (RsvgHandle * handle, cairo_t * cr, const char *id)
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
     g_return_val_if_fail (cr != NULL, FALSE);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return FALSE;
-    }
-
     return rsvg_handle_rust_render_cairo_sub (handle, cr, id);
 }
 
@@ -1031,10 +1026,6 @@ rsvg_handle_get_dimensions (RsvgHandle * handle, RsvgDimensionData * dimension_d
     g_return_if_fail (RSVG_IS_HANDLE (handle));
     g_return_if_fail (dimension_data != NULL);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return;
-    }
-
     rsvg_handle_rust_get_dimensions (handle, dimension_data);
 }
 
@@ -1058,10 +1049,6 @@ rsvg_handle_get_dimensions_sub (RsvgHandle * handle, RsvgDimensionData * dimensi
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
     g_return_val_if_fail (dimension_data, FALSE);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return FALSE;
-    }
-
     return rsvg_handle_rust_get_dimensions_sub (handle, dimension_data, id);
 }
 
@@ -1085,10 +1072,6 @@ rsvg_handle_get_geometry_sub (RsvgHandle * handle, RsvgRectangle * ink_rect, Rsv
 {
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return FALSE;
-    }
-
     return rsvg_handle_rust_get_geometry_sub(handle, ink_rect, logical_rect, id);
 }
 
@@ -1112,10 +1095,6 @@ rsvg_handle_get_position_sub (RsvgHandle * handle, RsvgPositionData * position_d
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
     g_return_val_if_fail (position_data != NULL, FALSE);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return FALSE;
-    }
-
     return rsvg_handle_rust_get_position_sub (handle, position_data, id);
 }
 
@@ -1136,10 +1115,6 @@ rsvg_handle_has_sub (RsvgHandle *handle,
 {
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return FALSE;
-    }
-
     return rsvg_handle_rust_has_sub (handle, id);
 }
 
@@ -1170,10 +1145,6 @@ rsvg_handle_get_pixbuf_sub (RsvgHandle * handle, const char *id)
 {
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), NULL);
 
-    if (!rsvg_handle_rust_is_loaded (handle)) {
-        return NULL;
-    }
-
     return rsvg_handle_rust_get_pixbuf_sub (handle, id);
 }
 
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index d5f1a6bb..4dc3e5af 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -875,31 +875,26 @@ pub unsafe extern "C" fn rsvg_handle_rust_is_at_start_for_setting_base_file(
     }
 }
 
-#[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_is_loaded(
-    handle: *const RsvgHandle,
-) -> glib_sys::gboolean {
-    let rhandle = get_rust_handle(handle);
-
-    match rhandle.load_state.get() {
+fn is_loaded(handle: &Handle) -> bool {
+    match handle.load_state.get() {
         LoadState::Start => {
             rsvg_g_warning("RsvgHandle has not been loaded");
-            false.to_glib()
+            false
         }
 
         LoadState::Loading => {
             rsvg_g_warning("RsvgHandle is still loading; call rsvg_handle_close() first");
-            false.to_glib()
+            false
         }
 
-        LoadState::ClosedOk => true.to_glib(),
+        LoadState::ClosedOk => true,
 
         LoadState::ClosedError => {
             rsvg_g_warning(
                 "RsvgHandle could not read or parse the SVG; did you check for errors during the \
                  loading stage?",
             );
-            false.to_glib()
+            false
         }
     }
 }
@@ -979,6 +974,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_geometry_sub(
 ) -> glib_sys::gboolean {
     let rhandle = get_rust_handle(handle);
 
+    if !is_loaded(rhandle) {
+        return false.to_glib();
+    }
+
     let id: Option<String> = from_glib_none(id);
 
     match rhandle.get_geometry_sub(handle, id.as_ref().map(String::as_str)) {
@@ -1014,15 +1013,18 @@ pub unsafe extern "C" fn rsvg_handle_rust_has_sub(
     handle: *mut RsvgHandle,
     id: *const libc::c_char,
 ) -> glib_sys::gboolean {
-    if id.is_null() {
-        false.to_glib()
-    } else {
-        let id: String = from_glib_none(id);
+    let rhandle = get_rust_handle(handle);
 
-        let rhandle = get_rust_handle(handle);
+    if !is_loaded(rhandle) {
+        return false.to_glib();
+    }
 
-        rhandle.has_sub(handle, &id).to_glib()
+    if id.is_null() {
+        return false.to_glib();
     }
+
+    let id: String = from_glib_none(id);
+    rhandle.has_sub(handle, &id).to_glib()
 }
 
 #[no_mangle]
@@ -1035,6 +1037,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_render_cairo_sub(
     let cr = from_glib_none(cr);
     let id: Option<String> = from_glib_none(id);
 
+    if !is_loaded(rhandle) {
+        return false.to_glib();
+    }
+
     match rhandle.render_cairo_sub(handle, &cr, id.as_ref().map(String::as_str)) {
         Ok(()) => true.to_glib(),
 
@@ -1051,9 +1057,12 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_pixbuf_sub(
     id: *const libc::c_char,
 ) -> *mut gdk_pixbuf_sys::GdkPixbuf {
     let rhandle = get_rust_handle(handle);
-
     let id: Option<String> = from_glib_none(id);
 
+    if !is_loaded(rhandle) {
+        return ptr::null_mut();
+    }
+
     match rhandle.get_pixbuf_sub(handle, id.as_ref().map(String::as_str)) {
         Ok(pixbuf) => pixbuf.to_glib_full(),
         Err(_) => ptr::null_mut(),
@@ -1067,6 +1076,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_dimensions(
 ) {
     let rhandle = get_rust_handle(handle);
 
+    if !is_loaded(rhandle) {
+        return;
+    }
+
     // This function is probably called from the cairo_render functions.
     // To prevent an infinite loop we are saving the state.
     if rhandle.in_loop.get() {
@@ -1091,6 +1104,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_dimensions_sub(
 ) -> glib_sys::gboolean {
     let rhandle = get_rust_handle(handle);
 
+    if !is_loaded(rhandle) {
+        return false.to_glib();
+    }
+
     let mut ink_r = RsvgRectangle {
         x: 0.0,
         y: 0.0,
@@ -1128,6 +1145,12 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_position_sub(
     position: *mut RsvgPositionData,
     id: *const libc::c_char,
 ) -> glib_sys::gboolean {
+    let rhandle = get_rust_handle(handle);
+
+    if !is_loaded(rhandle) {
+        return false.to_glib();
+    }
+
     // Short-cut when no id is given
     if id.is_null() || *id == 0 {
         (*position).x = 0;
@@ -1135,8 +1158,6 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_position_sub(
         return true.to_glib();
     }
 
-    let rhandle = get_rust_handle(handle);
-
     let mut ink_r = RsvgRectangle {
         x: 0.0,
         y: 0.0,
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 09641fed..e56d5d85 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -50,7 +50,6 @@ pub use handle::{
     rsvg_handle_rust_get_position_sub,
     rsvg_handle_rust_has_sub,
     rsvg_handle_rust_is_at_start_for_setting_base_file,
-    rsvg_handle_rust_is_loaded,
     rsvg_handle_rust_new,
     rsvg_handle_rust_read_stream_sync,
     rsvg_handle_rust_render_cairo_sub,


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