[librsvg] rsvg_handle_new_from_gfile_sync(): Port the implementation to Rust



commit 24efff7b3e11e633d0eb10cdd5623b09dfbc791a
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jan 9 13:20:38 2019 -0600

    rsvg_handle_new_from_gfile_sync(): Port the implementation to Rust

 librsvg/rsvg-handle.c        | 17 +++++------------
 rsvg_internals/src/handle.rs | 36 ++++++++++++++++++++++++++++++++++++
 rsvg_internals/src/lib.rs    |  1 +
 3 files changed, 42 insertions(+), 12 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 1a6168c1..a1e46a30 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -181,6 +181,10 @@ extern void rsvg_handle_rust_set_size_callback (RsvgHandleRust *raw_handle,
                                                 RsvgSizeFunc size_func,
                                                 gpointer user_data,
                                                 GDestroyNotify destroy_notify);
+extern RsvgHandle *rsvg_handle_rust_new_from_gfile_sync (GFile *file,
+                                                         RsvgHandleFlags flags,
+                                                         GCancellable *cancellable,
+                                                         GError **error);
 extern RsvgHandle *rsvg_handle_rust_new_from_stream_sync (GInputStream *input_stream,
                                                           GFile *base_file,
                                                           RsvgHandleFlags flags,
@@ -574,22 +578,11 @@ rsvg_handle_new_from_gfile_sync (GFile          *file,
                                  GCancellable   *cancellable,
                                  GError        **error)
 {
-    RsvgHandle *handle;
-    GFileInputStream *stream;
-
     g_return_val_if_fail (G_IS_FILE (file), NULL);
     g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
     g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-    stream = g_file_read (file, cancellable, error);
-    if (stream == NULL)
-        return NULL;
-
-    handle = rsvg_handle_new_from_stream_sync (G_INPUT_STREAM (stream), file,
-                                               flags, cancellable, error);
-    g_object_unref (stream);
-
-    return handle;
+    return rsvg_handle_rust_new_from_gfile_sync (file, flags, cancellable, error);
 }
 
 /**
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 7b5893e9..65206286 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -12,6 +12,7 @@ use gdk_pixbuf_sys;
 use gio::{self, FileExt};
 use gio_sys;
 use glib::translate::*;
+use glib::Cast;
 use glib_sys;
 use gobject_sys;
 use libc;
@@ -539,6 +540,16 @@ impl Handle {
         Ok(pixbuf)
     }
 
+    fn construct_new_from_gfile_sync(
+        &mut self,
+        handle: *mut RsvgHandle,
+        file: &gio::File,
+        cancellable: Option<&gio::Cancellable>,
+    ) -> Result<(), LoadingError> {
+        let stream = file.read(cancellable)?;
+        self.construct_read_stream_sync(handle, &stream.upcast(), Some(file), cancellable)
+    }
+
     fn construct_read_stream_sync(
         &mut self,
         handle: *mut RsvgHandle,
@@ -1174,6 +1185,31 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_position_sub(
     res
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_handle_rust_new_from_gfile_sync(
+    file: *mut gio_sys::GFile,
+    flags: u32,
+    cancellable: *mut gio_sys::GCancellable,
+    error: *mut *mut glib_sys::GError,
+) -> *mut RsvgHandle {
+    let raw_handle = rsvg_handle_new_with_flags(flags);
+
+    let rhandle = get_rust_handle(raw_handle);
+
+    let file = from_glib_none(file);
+    let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
+
+    match rhandle.construct_new_from_gfile_sync(raw_handle, &file, cancellable.as_ref()) {
+        Ok(()) => raw_handle,
+
+        Err(e) => {
+            set_gerror(error, 0, &format!("{}", e));
+            gobject_sys::g_object_unref(raw_handle as *mut _);
+            ptr::null_mut()
+        }
+    }
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_new_from_stream_sync(
     input_stream: *mut gio_sys::GInputStream,
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index f13be162..e2ccf936 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -54,6 +54,7 @@ pub use handle::{
     rsvg_handle_rust_get_position_sub,
     rsvg_handle_rust_has_sub,
     rsvg_handle_rust_new,
+    rsvg_handle_rust_new_from_gfile_sync,
     rsvg_handle_rust_new_from_stream_sync,
     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]