[librsvg: 12/45] Session: move the fields to an inner Arc<T>




commit 2e5b95385838682aa3fe0edd119f2cb39a816f79
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Aug 22 13:45:21 2022 -0500

    Session: move the fields to an inner Arc<T>
    
    It's going to be miserable to deref this everywhere, especially once
    we start having a mutex in there.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/731>

 src/api.rs     |  9 ++++-----
 src/handle.rs  |  6 ++----
 src/session.rs | 15 ++++++++++++++-
 3 files changed, 20 insertions(+), 10 deletions(-)
---
diff --git a/src/api.rs b/src/api.rs
index b8ea4e879..33729999b 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -13,7 +13,6 @@ 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;
@@ -36,7 +35,7 @@ use crate::{
 pub struct Loader {
     unlimited_size: bool,
     keep_image_data: bool,
-    session: Arc<Session>,
+    session: Session,
 }
 
 impl Loader {
@@ -60,14 +59,14 @@ impl Loader {
     ///     .unwrap();
     /// ```
     pub fn new() -> Self {
-        Self::new_with_session(Arc::new(Session::new()))
+        Self::new_with_session(Session::new())
     }
 
     /// Creates a `Loader` from a pre-created [`Session`].
     ///
     /// This is useful when a `Loader` must be created by the C API, which should already
     /// have created a session for logging.
-    pub(crate) fn new_with_session(session: Arc<Session>) -> Self {
+    pub(crate) fn new_with_session(session: Session) -> Self {
         Self {
             unlimited_size: false,
             keep_image_data: false,
@@ -233,7 +232,7 @@ fn url_from_file(file: &gio::File) -> Result<Url, LoadingError> {
 /// You can create this from one of the `read` methods in
 /// [`Loader`].
 pub struct SvgHandle {
-    session: Arc<Session>,
+    session: Session,
     pub(crate) handle: Handle,
 }
 
diff --git a/src/handle.rs b/src/handle.rs
index 541e38777..99827816b 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -2,8 +2,6 @@
 //!
 //! 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};
@@ -82,14 +80,14 @@ impl LoadOptions {
 ///
 /// [`from_stream`]: #method.from_stream
 pub struct Handle {
-    session: Arc<Session>,
+    session: Session,
     document: Document,
 }
 
 impl Handle {
     /// Loads an SVG document into a `Handle`.
     pub fn from_stream(
-        session: Arc<Session>,
+        session: Session,
         load_options: &LoadOptions,
         stream: &gio::InputStream,
         cancellable: Option<&gio::Cancellable>,
diff --git a/src/session.rs b/src/session.rs
index 8988f2fd2..e363e8e7f 100644
--- a/src/session.rs
+++ b/src/session.rs
@@ -1,5 +1,7 @@
 //! Tracks metadata for a loading/rendering session.
 
+use std::sync::Arc;
+
 use crate::log;
 
 /// Metadata for a loading/rendering session.
@@ -7,14 +9,25 @@ use crate::log;
 /// When the calling program first uses one of the API entry points (e.g. `Loader::new()`
 /// in the Rust API, or `rsvg_handle_new()` in the C API), there is no context yet where
 /// librsvg's code may start to track things.  This struct provides that context.
+#[derive(Clone)]
 pub struct Session {
+    inner: Arc<SessionInner>,
+}
+
+struct SessionInner {
     log_enabled: bool,
 }
 
 impl Session {
     pub fn new() -> Self {
         Self {
-            log_enabled: log::log_enabled(),
+            inner: Arc::new(SessionInner {
+                log_enabled: log::log_enabled(),
+            }),
         }
     }
+
+    pub fn log_enabled(&self) -> bool {
+        self.inner.log_enabled
+    }
 }


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