[librsvg: 12/17] document.rs: Pass around a single Arc<LoadOptions> instead of cloning it




commit dead6c9bb940b4568dc1f496c2f45c14c7f11e94
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Aug 29 20:19:01 2022 -0500

    document.rs: Pass around a single Arc<LoadOptions> instead of cloning it
    
    We'll need that LoadOptions for the XML parser, too.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/738>

 src/api.rs      |  3 ++-
 src/document.rs | 19 +++++++++++--------
 src/handle.rs   |  4 +++-
 3 files changed, 16 insertions(+), 10 deletions(-)
---
diff --git a/src/api.rs b/src/api.rs
index ee8953102..9079278ca 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -13,6 +13,7 @@ pub use crate::{
 use url::Url;
 
 use std::path::Path;
+use std::sync::Arc;
 
 use gio::prelude::*; // Re-exposes glib's prelude as well
 use gio::Cancellable;
@@ -218,7 +219,7 @@ impl Loader {
         Ok(SvgHandle {
             handle: Handle::from_stream(
                 self.session.clone(),
-                &load_options,
+                Arc::new(load_options),
                 stream.as_ref(),
                 cancellable.map(|c| c.as_ref()),
             )?,
diff --git a/src/document.rs b/src/document.rs
index 7f5ee8164..88281a4b0 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -11,6 +11,7 @@ use std::fmt;
 use std::include_str;
 use std::rc::Rc;
 use std::str::FromStr;
+use std::sync::Arc;
 
 use crate::css::{self, Origin, Stylesheet};
 use crate::error::{AcquireError, AllowedUrlError, LoadingError, NodeIdError};
@@ -90,7 +91,7 @@ pub struct Document {
     images: RefCell<Images>,
 
     /// Used to load referenced resources.
-    load_options: LoadOptions,
+    load_options: Arc<LoadOptions>,
 
     /// Stylesheets defined in the document.
     stylesheets: Vec<Stylesheet>,
@@ -100,13 +101,15 @@ impl Document {
     /// Constructs a `Document` by loading it from a stream.
     pub fn load_from_stream(
         session: Session,
-        load_options: &LoadOptions,
+        load_options: Arc<LoadOptions>,
         stream: &gio::InputStream,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Document, LoadingError> {
+        let unlimited_size = load_options.unlimited_size;
+
         xml_load_from_possibly_compressed_stream(
             DocumentBuilder::new(session, load_options),
-            load_options.unlimited_size,
+            unlimited_size,
             stream,
             cancellable,
         )
@@ -122,7 +125,7 @@ impl Document {
 
         Document::load_from_stream(
             Session::new_for_test_suite(),
-            &LoadOptions::new(UrlResolver::new(None)),
+            Arc::new(LoadOptions::new(UrlResolver::new(None))),
             &stream.upcast(),
             None::<&gio::Cancellable>,
         )
@@ -220,7 +223,7 @@ impl Resources {
                     .and_then(|stream| {
                         Document::load_from_stream(
                             session.clone(),
-                            &load_options.copy_with_base_url(aurl),
+                            Arc::new(load_options.copy_with_base_url(aurl)),
                             &stream,
                             None,
                         )
@@ -508,7 +511,7 @@ pub struct DocumentBuilder {
     session: Session,
 
     /// Loading options; mainly the URL resolver.
-    load_options: LoadOptions,
+    load_options: Arc<LoadOptions>,
 
     /// Root node of the tree.
     tree: Option<Node>,
@@ -521,10 +524,10 @@ pub struct DocumentBuilder {
 }
 
 impl DocumentBuilder {
-    pub fn new(session: Session, load_options: &LoadOptions) -> DocumentBuilder {
+    pub fn new(session: Session, load_options: Arc<LoadOptions>) -> DocumentBuilder {
         DocumentBuilder {
             session,
-            load_options: load_options.clone(),
+            load_options,
             tree: None,
             ids: HashMap::new(),
             stylesheets: Vec::new(),
diff --git a/src/handle.rs b/src/handle.rs
index 6fa7e5bb3..3ef89c209 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -2,6 +2,8 @@
 //!
 //! This module provides the primitives on which the public APIs are implemented.
 
+use std::sync::Arc;
+
 use crate::accept_language::UserLanguage;
 use crate::bbox::BoundingBox;
 use crate::css::{Origin, Stylesheet};
@@ -88,7 +90,7 @@ impl Handle {
     /// Loads an SVG document into a `Handle`.
     pub fn from_stream(
         session: Session,
-        load_options: &LoadOptions,
+        load_options: Arc<LoadOptions>,
         stream: &gio::InputStream,
         cancellable: Option<&gio::Cancellable>,
     ) -> Result<Handle, LoadingError> {


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