[librsvg] handle: pass around LoadOptions instead of the raw handle
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] handle: pass around LoadOptions instead of the raw handle
- Date: Sat, 12 Jan 2019 17:33:54 +0000 (UTC)
commit aef33a89fb8e1d2ba27b6cdebecadcdc956ee208
Author: Paolo Borelli <pborelli gnome org>
Date: Sat Jan 12 17:19:55 2019 +0100
handle: pass around LoadOptions instead of the raw handle
rsvg_internals/src/handle.rs | 47 ++++++++++++-----------------------------
rsvg_internals/src/load.rs | 12 +++++------
rsvg_internals/src/svg.rs | 15 +++++--------
rsvg_internals/src/xml.rs | 29 ++++++++++++-------------
rsvg_internals/src/xml2_load.rs | 19 ++++++++---------
5 files changed, 47 insertions(+), 75 deletions(-)
---
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 95c51766..b6d1bd77 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -1,5 +1,5 @@
use std;
-use std::cell::{Cell, Ref, RefCell};
+use std::cell::{Cell, RefCell};
use std::ffi::CString;
use std::mem;
use std::ptr;
@@ -201,17 +201,15 @@ impl Handle {
pub fn read_stream_sync(
&mut self,
- handle: *mut RsvgHandle,
stream: &gio::InputStream,
cancellable: Option<&gio::Cancellable>,
) -> Result<(), LoadingError> {
self.load_state.set(LoadState::Loading);
- let svg = Svg::load_from_stream(&self.load_options(), handle, stream, cancellable)
- .map_err(|e| {
- self.load_state.set(LoadState::ClosedError);
- e
- })?;
+ let svg = Svg::load_from_stream(self.load_options(), stream, cancellable).map_err(|e| {
+ self.load_state.set(LoadState::ClosedError);
+ e
+ })?;
*self.svg.borrow_mut() = Some(Rc::new(svg));
self.load_state.set(LoadState::ClosedOk);
@@ -222,7 +220,7 @@ impl Handle {
LoadOptions::new(self.load_flags.get(), self.base_url.borrow().clone())
}
- pub fn write(&mut self, handle: *mut RsvgHandle, buf: &[u8]) {
+ pub fn write(&mut self, buf: &[u8]) {
assert!(
self.load_state.get() == LoadState::Start
|| self.load_state.get() == LoadState::Loading
@@ -231,7 +229,7 @@ impl Handle {
if self.load_state.get() == LoadState::Start {
self.load_state.set(LoadState::Loading);
- self.load = RefCell::new(Some(LoadContext::new(handle, self.load_options())));
+ self.load = RefCell::new(Some(LoadContext::new(self.load_options())));
}
assert!(self.load_state.get() == LoadState::Loading);
@@ -539,17 +537,15 @@ impl Handle {
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)
+ self.construct_read_stream_sync(&stream.upcast(), Some(file), cancellable)
}
fn construct_read_stream_sync(
&mut self,
- handle: *mut RsvgHandle,
stream: &gio::InputStream,
base_file: Option<&gio::File>,
cancellable: Option<&gio::Cancellable>,
@@ -558,7 +554,7 @@ impl Handle {
self.set_base_gfile(file);
}
- self.read_stream_sync(handle, stream, cancellable)
+ self.read_stream_sync(stream, cancellable)
}
}
@@ -566,12 +562,6 @@ impl Handle {
const RSVG_HANDLE_FLAG_UNLIMITED: u32 = 1 << 0;
const RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA: u32 = 1 << 1;
-pub fn get_load_options(handle: *const RsvgHandle) -> LoadOptions {
- let rhandle = get_rust_handle(handle);
-
- rhandle.load_options()
-}
-
impl LoadFlags {
pub fn from_flags(flags: u32) -> Self {
LoadFlags {
@@ -638,12 +628,6 @@ pub fn load_extern(load_options: &LoadOptions, aurl: &AllowedUrl) -> Result<*mut
}
}
-pub fn get_base_url<'a>(handle: *const RsvgHandle) -> Ref<'a, Option<Url>> {
- let rhandle = get_rust_handle(handle);
-
- rhandle.base_url.borrow()
-}
-
pub fn load_image_to_surface(
load_options: &LoadOptions,
url: &str,
@@ -882,7 +866,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_read_stream_sync(
let stream = from_glib_none(stream);
let cancellable: Option<gio::Cancellable> = from_glib_none(cancellable);
- match rhandle.read_stream_sync(handle, &stream, cancellable.as_ref()) {
+ match rhandle.read_stream_sync(&stream, cancellable.as_ref()) {
Ok(()) => true.to_glib(),
Err(e) => {
@@ -909,7 +893,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_write(
let buffer = slice::from_raw_parts(buf, count);
- rhandle.write(handle, buffer);
+ rhandle.write(buffer);
}
#[no_mangle]
@@ -1157,7 +1141,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_new_from_gfile_sync(
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()) {
+ match rhandle.construct_new_from_gfile_sync(&file, cancellable.as_ref()) {
Ok(()) => raw_handle,
Err(e) => {
@@ -1184,12 +1168,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_new_from_stream_sync(
let stream = from_glib_none(input_stream);
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(),
- ) {
+ match rhandle.construct_read_stream_sync(&stream, base_file.as_ref(), cancellable.as_ref()) {
Ok(()) => raw_handle,
Err(e) => {
diff --git a/rsvg_internals/src/load.rs b/rsvg_internals/src/load.rs
index 0c9b1624..ae38a07a 100644
--- a/rsvg_internals/src/load.rs
+++ b/rsvg_internals/src/load.rs
@@ -1,7 +1,7 @@
use gio;
use glib::{Bytes, Cast};
-use handle::{LoadOptions, RsvgHandle};
+use handle::{LoadFlags, LoadOptions};
use xml::XmlState;
use xml2_load::{xml_state_load_from_possibly_compressed_stream, ParseFromStreamError};
@@ -14,7 +14,7 @@ use xml2_load::{xml_state_load_from_possibly_compressed_stream, ParseFromStreamE
// This struct maintains the loading context while an RsvgHandle is being
// populated with data, in case the caller is using write()/close().
pub struct LoadContext {
- load_options: LoadOptions,
+ load_flags: LoadFlags,
state: LoadState,
@@ -31,12 +31,12 @@ enum LoadState {
}
impl LoadContext {
- pub fn new(handle: *mut RsvgHandle, load_options: LoadOptions) -> LoadContext {
+ pub fn new(load_options: LoadOptions) -> LoadContext {
LoadContext {
- load_options,
+ load_flags: load_options.flags,
state: LoadState::Start,
buffer: Vec::new(),
- xml: Some(XmlState::new(handle)),
+ xml: Some(XmlState::new(load_options)),
}
}
@@ -70,7 +70,7 @@ impl LoadContext {
xml_state_load_from_possibly_compressed_stream(
self.xml.as_mut().unwrap(),
- &self.load_options,
+ self.load_flags,
&stream.upcast(),
None,
)
diff --git a/rsvg_internals/src/svg.rs b/rsvg_internals/src/svg.rs
index 1c7efa8d..4866392f 100644
--- a/rsvg_internals/src/svg.rs
+++ b/rsvg_internals/src/svg.rs
@@ -42,19 +42,14 @@ impl Svg {
}
pub fn load_from_stream(
- load_options: &LoadOptions,
- handle: *mut RsvgHandle,
+ load_options: LoadOptions,
stream: &gio::InputStream,
cancellable: Option<&gio::Cancellable>,
) -> Result<Svg, LoadingError> {
- let mut xml = XmlState::new(handle);
-
- xml_state_load_from_possibly_compressed_stream(
- &mut xml,
- &load_options,
- stream,
- cancellable,
- )?;
+ let load_flags = load_options.flags;
+ let mut xml = XmlState::new(load_options);
+
+ xml_state_load_from_possibly_compressed_stream(&mut xml, load_flags, stream, cancellable)?;
xml.validate_tree()?;
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 61a2987b..fd1d8a5a 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -12,7 +12,7 @@ use attributes::Attribute;
use create_node::create_node_and_register_id;
use css::CssStyles;
use error::LoadingError;
-use handle::{self, RsvgHandle};
+use handle::LoadOptions;
use io;
use node::{node_new, Node, NodeType, RsvgNode};
use property_bag::PropertyBag;
@@ -77,7 +77,7 @@ pub struct XmlState {
entities: HashMap<String, XmlEntityPtr>,
- handle: *mut RsvgHandle,
+ load_options: LoadOptions,
}
/// Errors returned from XmlState::acquire()
@@ -93,7 +93,7 @@ enum AcquireError {
}
impl XmlState {
- pub fn new(handle: *mut RsvgHandle) -> XmlState {
+ pub fn new(load_options: LoadOptions) -> XmlState {
XmlState {
tree: None,
ids: Some(HashMap::new()),
@@ -101,7 +101,7 @@ impl XmlState {
context_stack: vec![Context::Start],
current_node: None,
entities: HashMap::new(),
- handle,
+ load_options,
}
}
@@ -129,7 +129,7 @@ impl XmlState {
Svg::new(
self.tree.take().unwrap(),
self.ids.take().unwrap(),
- handle::get_load_options(self.handle),
+ self.load_options.clone(),
)
}
@@ -215,10 +215,9 @@ impl XmlState {
&& type_.as_ref().map(String::as_str) == Some("text/css")
&& href.is_some()
{
- if let Ok(aurl) = AllowedUrl::from_href(
- &href.unwrap(),
- handle::get_base_url(self.handle).as_ref(),
- ) {
+ if let Ok(aurl) =
+ AllowedUrl::from_href(&href.unwrap(), self.load_options.base_url.as_ref())
+ {
// FIXME: handle CSS errors
let css_styles = self.css_styles.as_mut().unwrap();
let _ = css_styles.load_css(&aurl);
@@ -285,7 +284,7 @@ impl XmlState {
let css_styles = self.css_styles.as_mut().unwrap();
- css_styles.parse(handle::get_base_url(self.handle).clone(), &css_data);
+ css_styles.parse(self.load_options.base_url.clone(), &css_data);
}
self.current_node = node.get_parent();
@@ -341,7 +340,7 @@ impl XmlState {
// For now we load resources directly when parsing, but probably we should
// move this to a trasversal of the tree once we finished parsing
- new_node.resolve_resources(&handle::get_load_options(self.handle));
+ new_node.resolve_resources(&self.load_options);
new_node
}
@@ -417,8 +416,8 @@ impl XmlState {
encoding: Option<&str>,
) -> Result<(), AcquireError> {
if let Some(href) = href {
- let aurl = AllowedUrl::from_href(href, handle::get_base_url(self.handle).as_ref())
- .map_err(|e| {
+ let aurl =
+ AllowedUrl::from_href(href, self.load_options.base_url.as_ref()).map_err(|e| {
// FIXME: should AlloweUrlError::HrefParseError be a fatal error,
// not a resource error?
rsvg_log!("could not acquire \"{}\": {}", href, e);
@@ -488,10 +487,10 @@ impl XmlState {
_ => AcquireError::ResourceError,
})?;
- let load_options = handle::get_load_options(self.handle);
+ let flags = self.load_options.flags;
// FIXME: pass a cancellable
- xml_state_parse_from_stream(self, &load_options, stream, None).map_err(|e| match e {
+ xml_state_parse_from_stream(self, flags, stream, None).map_err(|e| match e {
ParseFromStreamError::CouldNotCreateXmlParser => AcquireError::FatalError,
ParseFromStreamError::IoError(_) => AcquireError::ResourceError,
ParseFromStreamError::XmlParseError(_) => AcquireError::FatalError,
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index 5807ac3a..d2dfb091 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -13,7 +13,7 @@ use std::str;
use glib::translate::*;
use error::LoadingError;
-use handle::LoadOptions;
+use handle::LoadFlags;
use io::get_input_stream_for_loading;
use property_bag::PropertyBag;
use util::utf8_cstr;
@@ -188,10 +188,10 @@ unsafe extern "C" fn sax_get_parameter_entity_cb(
sax_get_entity_cb(ctx, name)
}
-fn set_xml_parse_options(parser: xmlParserCtxtPtr, load_options: &LoadOptions) {
+fn set_xml_parse_options(parser: xmlParserCtxtPtr, load_flags: LoadFlags) {
let mut options: libc::c_int = XML_PARSE_NONET | XML_PARSE_BIG_LINES;
- if load_options.flags.unlimited_size {
+ if load_flags.unlimited_size {
options |= XML_PARSE_HUGE;
}
@@ -285,7 +285,7 @@ struct Xml2Parser {
impl Xml2Parser {
fn from_stream(
xml: &mut XmlState,
- load_options: &LoadOptions,
+ load_flags: LoadFlags,
stream: gio::InputStream,
cancellable: Option<&gio::Cancellable>,
) -> Result<Xml2Parser, ParseFromStreamError> {
@@ -321,7 +321,7 @@ impl Xml2Parser {
// stream_ctx_close function
Err(ParseFromStreamError::CouldNotCreateXmlParser)
} else {
- set_xml_parse_options(parser, load_options);
+ set_xml_parse_options(parser, load_flags);
Ok(Xml2Parser { parser, gio_error })
}
}
@@ -411,22 +411,21 @@ impl From<ParseFromStreamError> for LoadingError {
// for example, when including another XML file via xi:include.
pub fn xml_state_parse_from_stream(
xml: &mut XmlState,
- load_options: &LoadOptions,
+ load_flags: LoadFlags,
stream: gio::InputStream,
cancellable: Option<&gio::Cancellable>,
) -> Result<(), ParseFromStreamError> {
- Xml2Parser::from_stream(xml, load_options, stream, cancellable)
- .and_then(|parser| parser.parse())
+ Xml2Parser::from_stream(xml, load_flags, stream, cancellable).and_then(|parser| parser.parse())
}
pub fn xml_state_load_from_possibly_compressed_stream(
xml: &mut XmlState,
- load_options: &LoadOptions,
+ load_flags: LoadFlags,
stream: &gio::InputStream,
cancellable: Option<&gio::Cancellable>,
) -> Result<(), ParseFromStreamError> {
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)
+ xml_state_parse_from_stream(xml, load_flags, stream, cancellable)
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]