[librsvg] (446): Accept IsA<InputStream> instead of plain InputStream wherever possible



commit f365340e637d450a3a6b7f4a8b9d4abebb0d187f
Author: Jordan Petridis <jpetridis gnome org>
Date:   Mon Apr 1 04:00:47 2019 +0300

    (446): Accept IsA<InputStream> instead of plain InputStream wherever possible
    
    This should allow for a more flexible api and avoid the need of
    casting before using it.
    
    Close #446

 librsvg_crate/src/lib.rs           | 9 ++++-----
 librsvg_crate/tests/utils/mod.rs   | 8 +++-----
 rsvg_internals/src/c_api.rs        | 4 ++--
 rsvg_internals/src/handle.rs       | 6 +++---
 rsvg_internals/src/io.rs           | 6 +++---
 rsvg_internals/src/pixbuf_utils.rs | 3 +--
 rsvg_internals/src/svg.rs          | 5 +++--
 rsvg_internals/src/xml.rs          | 9 +++++----
 rsvg_internals/src/xml2_load.rs    | 7 ++++---
 9 files changed, 28 insertions(+), 29 deletions(-)
---
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index 7cfc7dcc..5896d698 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -89,14 +89,13 @@
 #![warn(unused)]
 use cairo;
 use gio;
-use glib;
+use glib::{self, prelude::*};
 use rsvg_internals;
 use url::Url;
 
 use std::path::Path;
 
 use gio::{Cancellable, FileExt};
-use glib::object::Cast;
 
 use rsvg_internals::{Dpi, Handle, LoadFlags};
 
@@ -258,7 +257,7 @@ impl Loader {
 
         let stream = file.read(cancellable)?;
 
-        self.read_stream(&stream.upcast(), Some(&file), cancellable_clone)
+        self.read_stream(&stream, Some(&file), cancellable_clone)
     }
 
     /// Reads an SVG stream from a `gio::InputStream`.
@@ -273,9 +272,9 @@ impl Loader {
     /// URL where this SVG got loaded from.
     ///
     /// The `cancellable` can be used to cancel loading from another thread.
-    pub fn read_stream<'a, P: Into<Option<&'a Cancellable>>>(
+    pub fn read_stream<'a, P: Into<Option<&'a Cancellable>>, S: IsA<gio::InputStream>>(
         self,
-        stream: &gio::InputStream,
+        stream: &S,
         base_file: Option<&gio::File>,
         cancellable: P,
     ) -> Result<SvgHandle, LoadingError> {
diff --git a/librsvg_crate/tests/utils/mod.rs b/librsvg_crate/tests/utils/mod.rs
index d2cfbf75..17cfcd23 100644
--- a/librsvg_crate/tests/utils/mod.rs
+++ b/librsvg_crate/tests/utils/mod.rs
@@ -1,8 +1,6 @@
 use cairo;
 use gio;
-use gio::MemoryInputStreamExt;
 use glib;
-use glib::Cast;
 
 use librsvg;
 use librsvg::{CairoRenderer, Loader, RenderingError, SvgHandle};
@@ -21,11 +19,11 @@ use self::compare_surfaces::compare_surfaces;
 pub use self::compare_surfaces::BufferDiff;
 
 pub fn load_svg(input: &'static [u8]) -> SvgHandle {
-    let stream = gio::MemoryInputStream::new();
-    stream.add_bytes(&glib::Bytes::from_static(input));
+    let bytes = glib::Bytes::from_static(input);
+    let stream = gio::MemoryInputStream::new_from_bytes(&bytes);
 
     Loader::new()
-        .read_stream(&stream.upcast(), None, None)
+        .read_stream(&stream, None, None)
         .unwrap()
 }
 
diff --git a/rsvg_internals/src/c_api.rs b/rsvg_internals/src/c_api.rs
index 0a7bde4f..addfcb62 100644
--- a/rsvg_internals/src/c_api.rs
+++ b/rsvg_internals/src/c_api.rs
@@ -905,7 +905,7 @@ pub unsafe extern "C" fn rsvg_rust_handle_read_stream_sync(
 ) -> glib_sys::gboolean {
     let rhandle = get_rust_handle(handle);
 
-    let stream = from_glib_none(stream);
+    let stream = gio::InputStream::from_glib_none(stream);
     let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
 
     match rhandle.read_stream_sync(&stream, cancellable.as_ref()) {
@@ -1140,7 +1140,7 @@ pub unsafe extern "C" fn rsvg_rust_handle_new_from_stream_sync(
         rhandle.set_base_gfile(&base_file);
     }
 
-    let stream = from_glib_none(input_stream);
+    let stream: gio::InputStream = from_glib_none(input_stream);
     let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
 
     match rhandle.read_stream_sync(&stream, cancellable.as_ref()) {
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index a2c7f50a..fdd7caaf 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -3,7 +3,7 @@ use std::rc::Rc;
 use cairo::{self, ImageSurface, Status};
 use gdk_pixbuf::Pixbuf;
 use gio;
-use glib;
+use glib::{self, IsA};
 use libc;
 use locale_config::{LanguageRange, Locale};
 
@@ -67,9 +67,9 @@ pub struct Handle {
 }
 
 impl Handle {
-    pub fn from_stream(
+    pub fn from_stream<S: IsA<gio::InputStream>>(
         load_options: &LoadOptions,
-        stream: &gio::InputStream,
+        stream: &S,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Handle, LoadingError> {
         Ok(Handle {
diff --git a/rsvg_internals/src/io.rs b/rsvg_internals/src/io.rs
index 72b5239f..196431d5 100644
--- a/rsvg_internals/src/io.rs
+++ b/rsvg_internals/src/io.rs
@@ -13,7 +13,7 @@ use gio::{
     ZlibCompressorFormat,
     ZlibDecompressor,
 };
-use glib::{self, Bytes as GBytes, Cast};
+use glib::{self, Bytes as GBytes, Cast, IsA};
 
 use crate::allowed_url::AllowedUrl;
 use crate::error::{LoadingError, RsvgError};
@@ -51,8 +51,8 @@ fn decode_data_uri(uri: &str) -> Result<BinaryData, LoadingError> {
 const GZ_MAGIC_0: u8 = 0x1f;
 const GZ_MAGIC_1: u8 = 0x8b;
 
-pub fn get_input_stream_for_loading(
-    stream: &InputStream,
+pub fn get_input_stream_for_loading<S: IsA<InputStream>> (
+    stream: &S,
     cancellable: Option<&Cancellable>,
 ) -> Result<InputStream, glib::Error> {
     // detect gzipped streams (svgz)
diff --git a/rsvg_internals/src/pixbuf_utils.rs b/rsvg_internals/src/pixbuf_utils.rs
index 1a5995cd..5a17f463 100644
--- a/rsvg_internals/src/pixbuf_utils.rs
+++ b/rsvg_internals/src/pixbuf_utils.rs
@@ -7,7 +7,6 @@ use gdk_pixbuf_sys;
 use gio;
 use gio::prelude::*;
 use glib::translate::*;
-use glib::Cast;
 use glib_sys;
 use libc;
 use url::Url;
@@ -229,7 +228,7 @@ fn pixbuf_from_file_with_size_mode(
         let handle = match file
             .read(cancellable)
             .map_err(|e| LoadingError::from(e))
-            .and_then(|stream| Handle::from_stream(&load_options, &stream.upcast(), None))
+            .and_then(|stream| Handle::from_stream(&load_options, &stream, None))
         {
             Ok(handle) => handle,
             Err(e) => {
diff --git a/rsvg_internals/src/svg.rs b/rsvg_internals/src/svg.rs
index 38141f52..edc1299c 100644
--- a/rsvg_internals/src/svg.rs
+++ b/rsvg_internals/src/svg.rs
@@ -1,5 +1,6 @@
 use gdk_pixbuf::{PixbufLoader, PixbufLoaderExt};
 use gio;
+use glib::IsA;
 use std::cell::RefCell;
 use std::collections::hash_map::Entry;
 use std::collections::HashMap;
@@ -48,9 +49,9 @@ impl Svg {
         }
     }
 
-    pub fn load_from_stream(
+    pub fn load_from_stream<S: IsA<gio::InputStream>>(
         load_options: &LoadOptions,
-        stream: &gio::InputStream,
+        stream: &S,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Svg, LoadingError> {
         let mut xml = XmlState::new(load_options);
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 69249133..a034f7dc 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -1,6 +1,7 @@
 use crate::xml_rs::{reader::XmlEvent, ParserConfig};
 use encoding::label::encoding_from_whatwg_label;
 use encoding::DecoderTrap;
+use glib::IsA;
 use libc;
 use std::collections::HashMap;
 use std::rc::Rc;
@@ -487,18 +488,18 @@ impl XmlState {
     //
     // This can be called "in the middle" of an XmlState's processing status,
     // for example, when including another XML file via xi:include.
-    fn parse_from_stream(
+    fn parse_from_stream<S: IsA<gio::InputStream>>(
         &mut self,
-        stream: gio::InputStream,
+        stream: S,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<(), ParseFromStreamError> {
         Xml2Parser::from_stream(self, self.load_options.flags, stream, cancellable)
             .and_then(|parser| parser.parse())
     }
 
-    pub fn load_from_possibly_compressed_stream(
+    pub fn load_from_possibly_compressed_stream<S: IsA<gio::InputStream>>(
         &mut self,
-        stream: &gio::InputStream,
+        stream: &S,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<(), ParseFromStreamError> {
         let stream = get_input_stream_for_loading(stream, cancellable)
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index eb6eb079..9092a757 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -3,6 +3,7 @@
 
 use gio;
 use gio::prelude::*;
+use glib::prelude::*;
 use std::borrow::Cow;
 use std::cell::RefCell;
 use std::mem;
@@ -317,10 +318,10 @@ pub struct Xml2Parser {
 }
 
 impl Xml2Parser {
-    pub fn from_stream(
+    pub fn from_stream<S: IsA<gio::InputStream>>(
         xml: &mut XmlState,
         load_flags: LoadFlags,
-        stream: gio::InputStream,
+        stream: S,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Xml2Parser, ParseFromStreamError> {
         init_libxml2();
@@ -335,7 +336,7 @@ impl Xml2Parser {
         let gio_error = Rc::new(RefCell::new(None));
 
         let ctx = Box::new(StreamCtx {
-            stream,
+            stream: stream.upcast(),
             cancellable: cancellable.map(|c| c.clone()),
             gio_error: gio_error.clone(),
         });


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