From 0d68293a947ff12c3edffb4e2ee7a8a3e372c661 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Wed, 9 Jan 2019 08:02:22 -0600 Subject: 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. --- rsvg_internals/Cargo.toml | 1 + rsvg_internals/src/handle.rs | 34 ++++++++++++++++++++++++++++++++++ rsvg_internals/src/lib.rs | 2 ++ 3 files changed, 37 insertions(+) (limited to 'rsvg_internals') 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, -- cgit v1.2.1