[librsvg: 3/5] Handle::construct_read_stream_sync() - Move code from the exported function into here



commit e4ba2f1962178a02ff58ebce7498643f8beaef5d
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jan 9 08:23:51 2019 -0600

    Handle::construct_read_stream_sync() - Move code from the exported function into here
    
    Let's see how it works to have the from_glib_() conversions in the
    pub extern functions, and to have the real work in Handle methods.
    
    Also, this commit changes some functions to take borrowed references
    to gio types (e.g. foo: &InputStream) instead of taking
    ownership (foo: InputStream).  The former follows the gtk-rs
    convention, and is the right thing anyway.

 rsvg_internals/src/handle.rs    | 49 +++++++++++++++++++++++++++--------------
 rsvg_internals/src/io.rs        |  4 ++--
 rsvg_internals/src/load.rs      |  2 +-
 rsvg_internals/src/svg.rs       |  4 ++--
 rsvg_internals/src/xml2_load.rs |  8 +++----
 5 files changed, 41 insertions(+), 26 deletions(-)
---
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index fef0afb5..7b5893e9 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -9,7 +9,7 @@ use cairo::{self, ImageSurface, Status};
 use cairo_sys;
 use gdk_pixbuf::{Colorspace, Pixbuf, PixbufLoader, PixbufLoaderExt};
 use gdk_pixbuf_sys;
-use gio::{File as GFile, FileExt};
+use gio::{self, FileExt};
 use gio_sys;
 use glib::translate::*;
 use glib_sys;
@@ -195,7 +195,7 @@ impl Handle {
         }
     }
 
-    fn set_base_gfile(&self, file: &GFile) {
+    fn set_base_gfile(&self, file: &gio::File) {
         if let Some(uri) = file.get_uri() {
             self.set_base_url(&uri);
         } else {
@@ -206,8 +206,8 @@ impl Handle {
     pub fn read_stream_sync(
         &mut self,
         handle: *mut RsvgHandle,
-        stream: gio::InputStream,
-        cancellable: Option<gio::Cancellable>,
+        stream: &gio::InputStream,
+        cancellable: Option<&gio::Cancellable>,
     ) -> Result<(), LoadingError> {
         self.load_state.set(LoadState::Loading);
 
@@ -538,6 +538,20 @@ impl Handle {
 
         Ok(pixbuf)
     }
+
+    fn construct_read_stream_sync(
+        &mut self,
+        handle: *mut RsvgHandle,
+        stream: &gio::InputStream,
+        base_file: Option<&gio::File>,
+        cancellable: Option<&gio::Cancellable>,
+    ) -> Result<(), LoadingError> {
+        if let Some(file) = base_file {
+            self.set_base_gfile(file);
+        }
+
+        self.read_stream_sync(handle, stream, cancellable)
+    }
 }
 
 // Keep these in sync with rsvg.h:RsvgHandleFlags
@@ -601,7 +615,7 @@ pub fn lookup_fragment_id(handle: *const RsvgHandle, id: &str) -> Option<Rc<Node
 
 pub fn load_extern(load_options: &LoadOptions, aurl: &AllowedUrl) -> Result<*const RsvgHandle, ()> {
     unsafe {
-        let file = GFile::new_for_uri(aurl.url().as_str());
+        let file = gio::File::new_for_uri(aurl.url().as_str());
 
         let res = rsvg_handle_new_from_gfile_sync(
             file.to_glib_none().0,
@@ -758,7 +772,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_base_gfile(
 
     match *handle.base_url.borrow() {
         None => ptr::null_mut(),
-        Some(ref url) => GFile::new_for_uri(url.as_str()).to_glib_full(),
+        Some(ref url) => gio::File::new_for_uri(url.as_str()).to_glib_full(),
     }
 }
 
@@ -771,7 +785,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_set_base_gfile(
 
     assert!(!raw_gfile.is_null());
 
-    let file: GFile = from_glib_none(raw_gfile);
+    let file: gio::File = from_glib_none(raw_gfile);
 
     handle.set_base_gfile(&file);
 }
@@ -897,9 +911,9 @@ pub unsafe extern "C" fn rsvg_handle_rust_read_stream_sync(
     }
 
     let stream = from_glib_none(stream);
-    let cancellable = from_glib_none(cancellable);
+    let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
 
-    match rhandle.read_stream_sync(handle, stream, cancellable) {
+    match rhandle.read_stream_sync(handle, &stream, cancellable.as_ref()) {
         Ok(()) => true.to_glib(),
 
         Err(e) => {
@@ -1172,15 +1186,16 @@ pub unsafe extern "C" fn rsvg_handle_rust_new_from_stream_sync(
 
     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 base_file: Option<gio::File> = from_glib_none(base_file);
     let stream = from_glib_none(input_stream);
-    let cancellable = from_glib_none(cancellable);
-
-    match rhandle.read_stream_sync(raw_handle, stream, cancellable) {
+    let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
+
+    match rhandle.construct_read_stream_sync(
+        raw_handle,
+        &stream,
+        base_file.as_ref(),
+        cancellable.as_ref(),
+    ) {
         Ok(()) => raw_handle,
 
         Err(e) => {
diff --git a/rsvg_internals/src/io.rs b/rsvg_internals/src/io.rs
index 0744a58d..ea07d105 100644
--- a/rsvg_internals/src/io.rs
+++ b/rsvg_internals/src/io.rs
@@ -48,12 +48,12 @@ const GZ_MAGIC_0: u8 = 0x1f;
 const GZ_MAGIC_1: u8 = 0x8b;
 
 pub fn get_input_stream_for_loading(
-    stream: InputStream,
+    stream: &InputStream,
     cancellable: Option<&Cancellable>,
 ) -> Result<InputStream, glib::Error> {
     // detect gzipped streams (svgz)
 
-    let buffered = BufferedInputStream::new(&stream);
+    let buffered = BufferedInputStream::new(stream);
     let num_read = buffered.fill(2, cancellable)?;
     if num_read < 2 {
         // FIXME: this string was localized in the original; localize it
diff --git a/rsvg_internals/src/load.rs b/rsvg_internals/src/load.rs
index a4b0965e..0c9b1624 100644
--- a/rsvg_internals/src/load.rs
+++ b/rsvg_internals/src/load.rs
@@ -71,7 +71,7 @@ impl LoadContext {
                 xml_state_load_from_possibly_compressed_stream(
                     self.xml.as_mut().unwrap(),
                     &self.load_options,
-                    stream.upcast(),
+                    &stream.upcast(),
                     None,
                 )
                 .and_then(|_| Ok(self.xml.take().unwrap()))
diff --git a/rsvg_internals/src/svg.rs b/rsvg_internals/src/svg.rs
index 37c72bf2..eb9a81d2 100644
--- a/rsvg_internals/src/svg.rs
+++ b/rsvg_internals/src/svg.rs
@@ -51,8 +51,8 @@ impl Svg {
     pub fn load_from_stream(
         load_options: &LoadOptions,
         handle: *mut RsvgHandle,
-        stream: gio::InputStream,
-        cancellable: Option<gio::Cancellable>,
+        stream: &gio::InputStream,
+        cancellable: Option<&gio::Cancellable>,
     ) -> Result<Svg, LoadingError> {
         let mut xml = XmlState::new(handle);
 
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index 961ebd76..5807ac3a 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -422,11 +422,11 @@ pub fn xml_state_parse_from_stream(
 pub fn xml_state_load_from_possibly_compressed_stream(
     xml: &mut XmlState,
     load_options: &LoadOptions,
-    stream: gio::InputStream,
-    cancellable: Option<gio::Cancellable>,
+    stream: &gio::InputStream,
+    cancellable: Option<&gio::Cancellable>,
 ) -> Result<(), ParseFromStreamError> {
-    let stream = get_input_stream_for_loading(stream, cancellable.as_ref())
+    let stream = get_input_stream_for_loading(stream, cancellable)
         .map_err(|e| ParseFromStreamError::IoError(e))?;
 
-    xml_state_parse_from_stream(xml, load_options, stream, cancellable.as_ref())
+    xml_state_parse_from_stream(xml, load_options, stream, cancellable)
 }


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