[librsvg: 1/5] rsvg_handle_new_from_stream_sync(): Port to Rust



commit 0d68293a947ff12c3edffb4e2ee7a8a3e372c661
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jan 9 08:02:22 2019 -0600

    rsvg_handle_new_from_stream_sync(): Port to Rust
    
    This is kind of horrible right now, as the rustified version is harder
    to read.  However, I think porting these convenience constructors will
    make it easier to see a better pattern for Rust construction in
    general... and to integrate rsvg-rs into the build.

 Cargo.lock                   |  1 +
 librsvg/rsvg-handle.c        | 23 ++++++++++-------------
 rsvg_internals/Cargo.toml    |  1 +
 rsvg_internals/src/handle.rs | 34 ++++++++++++++++++++++++++++++++++
 rsvg_internals/src/lib.rs    |  2 ++
 5 files changed, 48 insertions(+), 13 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 2757a749..734d79d9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -962,6 +962,7 @@ dependencies = [
  "gio-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "glib 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "glib-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "gobject-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 6bf18786..1a6168c1 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -181,6 +181,11 @@ 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_stream_sync (GInputStream *input_stream,
+                                                          GFile *base_file,
+                                                          RsvgHandleFlags flags,
+                                                          GCancellable *cancellable,
+                                                          GError **error);
 
 struct RsvgHandlePrivate {
     RsvgHandleRust *rust_handle;
@@ -613,24 +618,16 @@ rsvg_handle_new_from_stream_sync (GInputStream   *input_stream,
                                   GCancellable    *cancellable,
                                   GError         **error)
 {
-    RsvgHandle *handle;
-
     g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), NULL);
     g_return_val_if_fail (base_file == NULL || G_IS_FILE (base_file), NULL);
     g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
     g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-    handle = rsvg_handle_new_with_flags (flags);
-
-    if (base_file)
-        rsvg_handle_set_base_gfile (handle, base_file);
-
-    if (!rsvg_handle_read_stream_sync (handle, input_stream, cancellable, error)) {
-        g_object_unref (handle);
-        return NULL;
-    }
-
-    return handle;
+    return rsvg_handle_rust_new_from_stream_sync (input_stream,
+                                                  base_file,
+                                                  flags,
+                                                  cancellable,
+                                                  error);
 }
 
 /**
diff --git a/rsvg_internals/Cargo.toml b/rsvg_internals/Cargo.toml
index c9f077c2..268498b9 100644
--- a/rsvg_internals/Cargo.toml
+++ b/rsvg_internals/Cargo.toml
@@ -33,6 +33,7 @@ gio = { version="0.5.1", features=["v2_48"] } # per configure.ac
 gio-sys = "0.7.0"
 glib = "0.6.0"
 glib-sys = "0.7.0"
+gobject-sys = "0.7.0"
 itertools = "0.7.4"
 language-tags = "0.2.2"
 lazy_static = "1.0.0"
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 16497567..fef0afb5 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -13,6 +13,7 @@ use gio::{File as GFile, FileExt};
 use gio_sys;
 use glib::translate::*;
 use glib_sys;
+use gobject_sys;
 use libc;
 use url::Url;
 
@@ -574,6 +575,8 @@ impl LoadFlags {
 
 #[allow(improper_ctypes)]
 extern "C" {
+    fn rsvg_handle_new_with_flags(flags: u32) -> *mut RsvgHandle;
+
     fn rsvg_handle_new_from_gfile_sync(
         file: *const gio_sys::GFile,
         flags: u32,
@@ -1156,3 +1159,34 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_position_sub(
 
     res
 }
+
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_handle_rust_new_from_stream_sync(
+    input_stream: *mut gio_sys::GInputStream,
+    base_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);
+
+    if !base_file.is_null() {
+        let file: GFile = from_glib_none(base_file);
+        rhandle.set_base_gfile(&file);
+    }
+
+    let stream = from_glib_none(input_stream);
+    let cancellable = from_glib_none(cancellable);
+
+    match rhandle.read_stream_sync(raw_handle, stream, cancellable) {
+        Ok(()) => raw_handle,
+
+        Err(e) => {
+            set_gerror(error, 0, &format!("{}", e));
+            gobject_sys::g_object_unref(raw_handle as *mut _);
+            ptr::null_mut()
+        }
+    }
+}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 4ee9766c..f13be162 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -16,6 +16,7 @@ extern crate gio;
 extern crate gio_sys;
 extern crate glib;
 extern crate glib_sys;
+extern crate gobject_sys;
 extern crate itertools;
 extern crate language_tags;
 extern crate libc;
@@ -53,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_stream_sync,
     rsvg_handle_rust_read_stream_sync,
     rsvg_handle_rust_render_cairo_sub,
     rsvg_handle_rust_set_base_gfile,


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