[librsvg: 2/5] handle.rs: Take an RsvgHandle, not a Handle, into the functions exported to C



commit b97a871a0d919c5f2bed74464b7b5ffd43e9b373
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jan 16 16:34:00 2019 -0600

    handle.rs: Take an RsvgHandle, not a Handle, into the functions exported to C
    
    Now that all the private fields are in Rust, we can make the C code
    simpler by not extracting the priv->rust_handle itself, but rather
    leaving that job to the Rust functions.

 librsvg/rsvg-handle.c        | 76 +++++++++++++++-----------------------------
 rsvg_internals/src/handle.rs | 58 ++++++++++++++++-----------------
 2 files changed, 54 insertions(+), 80 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 2609f2e1..0fb6beb1 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -143,17 +143,17 @@ RsvgHandleRust *rsvg_handle_get_rust (RsvgHandle *handle);
 /* Implemented in rsvg_internals/src/handle.rs */
 extern RsvgHandleRust *rsvg_handle_rust_new (void);
 extern void rsvg_handle_rust_free (RsvgHandleRust *raw_handle);
-extern double rsvg_handle_rust_get_dpi_x (RsvgHandleRust *raw_handle);
-extern double rsvg_handle_rust_get_dpi_y (RsvgHandleRust *raw_handle);
-extern void rsvg_handle_rust_set_dpi_x (RsvgHandleRust *raw_handle, double dpi_x);
-extern void rsvg_handle_rust_set_dpi_y (RsvgHandleRust *raw_handle, double dpi_y);
-extern void rsvg_handle_rust_set_base_url (RsvgHandleRust *raw_handle, const char *uri);
-extern void rsvg_handle_rust_set_base_gfile (RsvgHandleRust *raw_handle, GFile *file);
-extern const char *rsvg_handle_rust_get_base_url (RsvgHandleRust *raw_handle);
-extern GFile *rsvg_handle_rust_get_base_gfile (RsvgHandleRust *raw_handle);
-extern guint rsvg_handle_rust_get_flags (RsvgHandleRust *raw_handle);
-extern void rsvg_handle_rust_set_flags (RsvgHandleRust *raw_handle, guint flags);
-extern guint rsvg_handle_rust_set_testing (RsvgHandleRust *raw_handle, gboolean testing);
+extern double rsvg_handle_rust_get_dpi_x (RsvgHandle *raw_handle);
+extern double rsvg_handle_rust_get_dpi_y (RsvgHandle *raw_handle);
+extern void rsvg_handle_rust_set_dpi_x (RsvgHandle *raw_handle, double dpi_x);
+extern void rsvg_handle_rust_set_dpi_y (RsvgHandle *raw_handle, double dpi_y);
+extern void rsvg_handle_rust_set_base_url (RsvgHandle *raw_handle, const char *uri);
+extern void rsvg_handle_rust_set_base_gfile (RsvgHandle *raw_handle, GFile *file);
+extern const char *rsvg_handle_rust_get_base_url (RsvgHandle *raw_handle);
+extern GFile *rsvg_handle_rust_get_base_gfile (RsvgHandle *raw_handle);
+extern guint rsvg_handle_rust_get_flags (RsvgHandle *raw_handle);
+extern void rsvg_handle_rust_set_flags (RsvgHandle *raw_handle, guint flags);
+extern guint rsvg_handle_rust_set_testing (RsvgHandle *raw_handle, gboolean testing);
 extern gboolean rsvg_handle_rust_read_stream_sync (RsvgHandle *handle,
                                                    GInputStream *stream,
                                                    GCancellable *cancellable,
@@ -177,7 +177,7 @@ extern gboolean rsvg_handle_rust_get_dimensions_sub (RsvgHandle *handle,
 extern gboolean rsvg_handle_rust_get_position_sub (RsvgHandle *handle,
                                                    RsvgPositionData *dimension_data,
                                                    const char *id);
-extern void rsvg_handle_rust_set_size_callback (RsvgHandleRust *raw_handle,
+extern void rsvg_handle_rust_set_size_callback (RsvgHandle *raw_handle,
                                                 RsvgSizeFunc size_func,
                                                 gpointer user_data,
                                                 GDestroyNotify destroy_notify);
@@ -242,17 +242,16 @@ static void
 rsvg_handle_set_property (GObject * instance, guint prop_id, GValue const *value, GParamSpec * pspec)
 {
     RsvgHandle *self = RSVG_HANDLE (instance);
-    RsvgHandlePrivate *priv = rsvg_handle_get_instance_private (self);
 
     switch (prop_id) {
     case PROP_FLAGS:
-        rsvg_handle_rust_set_flags (priv->rust_handle, g_value_get_flags (value));
+        rsvg_handle_rust_set_flags (self, g_value_get_flags (value));
         break;
     case PROP_DPI_X:
-        rsvg_handle_rust_set_dpi_x (priv->rust_handle, g_value_get_double (value));
+        rsvg_handle_rust_set_dpi_x (self, g_value_get_double (value));
         break;
     case PROP_DPI_Y:
-        rsvg_handle_rust_set_dpi_y (priv->rust_handle, g_value_get_double (value));
+        rsvg_handle_rust_set_dpi_y (self, g_value_get_double (value));
         break;
     case PROP_BASE_URI: {
         const char *str = g_value_get_string (value);
@@ -272,18 +271,17 @@ static void
 rsvg_handle_get_property (GObject * instance, guint prop_id, GValue * value, GParamSpec * pspec)
 {
     RsvgHandle *self = RSVG_HANDLE (instance);
-    RsvgHandlePrivate *priv = rsvg_handle_get_instance_private (self);
     RsvgDimensionData dim;
 
     switch (prop_id) {
     case PROP_FLAGS:
-        g_value_set_flags (value, rsvg_handle_rust_get_flags (priv->rust_handle));
+        g_value_set_flags (value, rsvg_handle_rust_get_flags (self));
         break;
     case PROP_DPI_X:
-        g_value_set_double (value, rsvg_handle_rust_get_dpi_x (priv->rust_handle));
+        g_value_set_double (value, rsvg_handle_rust_get_dpi_x (self));
         break;
     case PROP_DPI_Y:
-        g_value_set_double (value, rsvg_handle_rust_get_dpi_y (priv->rust_handle));
+        g_value_set_double (value, rsvg_handle_rust_get_dpi_y (self));
         break;
     case PROP_BASE_URI:
         g_value_set_string (value, rsvg_handle_get_base_uri (self));
@@ -697,14 +695,10 @@ rsvg_handle_read_stream_sync (RsvgHandle   *handle,
 void
 rsvg_handle_set_base_uri (RsvgHandle * handle, const char *base_uri)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_if_fail (RSVG_IS_HANDLE (handle));
     g_return_if_fail (base_uri != NULL);
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    rsvg_handle_rust_set_base_url (priv->rust_handle, base_uri);
+    rsvg_handle_rust_set_base_url (handle, base_uri);
 }
 
 /**
@@ -722,14 +716,10 @@ void
 rsvg_handle_set_base_gfile (RsvgHandle *handle,
                             GFile      *base_file)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_if_fail (RSVG_IS_HANDLE (handle));
     g_return_if_fail (G_IS_FILE (base_file));
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    rsvg_handle_rust_set_base_gfile (priv->rust_handle, base_file);
+    rsvg_handle_rust_set_base_gfile (handle, base_file);
 }
 
 /**
@@ -744,13 +734,9 @@ rsvg_handle_set_base_gfile (RsvgHandle *handle,
 const char *
 rsvg_handle_get_base_uri (RsvgHandle *handle)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_val_if_fail (RSVG_IS_HANDLE (handle), NULL);
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    return rsvg_handle_rust_get_base_url (priv->rust_handle);
+    return rsvg_handle_rust_get_base_url (handle);
 }
 
 /**
@@ -1048,14 +1034,10 @@ rsvg_handle_set_dpi (RsvgHandle * handle, double dpi)
 void
 rsvg_handle_set_dpi_x_y (RsvgHandle * handle, double dpi_x, double dpi_y)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_if_fail (RSVG_IS_HANDLE (handle));
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    rsvg_handle_rust_set_dpi_x (priv->rust_handle, dpi_x);
-    rsvg_handle_rust_set_dpi_y (priv->rust_handle, dpi_y);
+    rsvg_handle_rust_set_dpi_x (handle, dpi_x);
+    rsvg_handle_rust_set_dpi_y (handle, dpi_y);
 }
 
 /**
@@ -1104,13 +1086,9 @@ rsvg_handle_set_size_callback (RsvgHandle * handle,
                                gpointer user_data,
                                GDestroyNotify user_data_destroy)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_if_fail (RSVG_IS_HANDLE (handle));
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    rsvg_handle_rust_set_size_callback (priv->rust_handle,
+    rsvg_handle_rust_set_size_callback (handle,
                                         size_func,
                                         user_data,
                                         user_data_destroy);
@@ -1127,13 +1105,9 @@ rsvg_handle_set_size_callback (RsvgHandle * handle,
 void
 rsvg_handle_internal_set_testing (RsvgHandle *handle, gboolean testing)
 {
-    RsvgHandlePrivate *priv;
-
     g_return_if_fail (RSVG_IS_HANDLE (handle));
 
-    priv = rsvg_handle_get_instance_private (handle);
-
-    rsvg_handle_rust_set_testing (priv->rust_handle, testing);
+    rsvg_handle_rust_set_testing (handle, testing);
 }
 
 /* This one is defined in the C code, because the prototype has varargs
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index ca49adad..f42024bd 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -591,15 +591,15 @@ pub fn get_rust_handle<'a>(handle: *const RsvgHandle) -> &'a mut Handle {
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_set_base_url(
-    raw_handle: *const Handle,
+    raw_handle: *mut RsvgHandle,
     uri: *const libc::c_char,
 ) {
-    let handle = &*raw_handle;
+    let rhandle = get_rust_handle(raw_handle);
 
     assert!(!uri.is_null());
     let uri: String = from_glib_none(uri);
 
-    handle.set_base_url(&uri);
+    rhandle.set_base_url(&uri);
 }
 
 #[no_mangle]
@@ -616,80 +616,80 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_base_gfile(
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_set_base_gfile(
-    raw_handle: *const Handle,
+    raw_handle: *mut RsvgHandle,
     raw_gfile: *mut gio_sys::GFile,
 ) {
-    let handle = &*raw_handle;
+    let rhandle = get_rust_handle(raw_handle);
 
     assert!(!raw_gfile.is_null());
 
     let file: gio::File = from_glib_none(raw_gfile);
 
-    handle.set_base_gfile(&file);
+    rhandle.set_base_gfile(&file);
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_get_base_url(
-    raw_handle: *const Handle,
+    raw_handle: *const RsvgHandle,
 ) -> *const libc::c_char {
-    let handle = &*raw_handle;
+    let rhandle = get_rust_handle(raw_handle);
 
-    match *handle.base_url_cstring.borrow() {
+    match *rhandle.base_url_cstring.borrow() {
         None => ptr::null(),
         Some(ref url) => url.as_ptr(),
     }
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_x(raw_handle: *const Handle, dpi_x: f64) {
-    let handle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_x(raw_handle: *mut RsvgHandle, dpi_x: f64) {
+    let rhandle = get_rust_handle(raw_handle);
 
-    handle.dpi.set(Dpi::new(dpi_x, handle.dpi.get().y()));
+    rhandle.dpi.set(Dpi::new(dpi_x, rhandle.dpi.get().y()));
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_x(raw_handle: *const Handle) -> f64 {
-    let handle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_x(raw_handle: *const RsvgHandle) -> f64 {
+    let rhandle = get_rust_handle(raw_handle);
 
-    handle.dpi.get().x()
+    rhandle.dpi.get().x()
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_y(raw_handle: *const Handle, dpi_y: f64) {
-    let handle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_set_dpi_y(raw_handle: *mut RsvgHandle, dpi_y: f64) {
+    let rhandle = get_rust_handle(raw_handle);
 
-    handle.dpi.set(Dpi::new(handle.dpi.get().x(), dpi_y));
+    rhandle.dpi.set(Dpi::new(rhandle.dpi.get().x(), dpi_y));
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_y(raw_handle: *const Handle) -> f64 {
-    let handle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_get_dpi_y(raw_handle: *const RsvgHandle) -> f64 {
+    let rhandle = get_rust_handle(raw_handle);
 
-    handle.dpi.get().y()
+    rhandle.dpi.get().y()
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_get_flags(raw_handle: *const Handle) -> u32 {
-    let rhandle = &*raw_handle;
+pub unsafe extern "C" fn rsvg_handle_rust_get_flags(raw_handle: *const RsvgHandle) -> u32 {
+    let rhandle = get_rust_handle(raw_handle);
 
     rhandle.load_flags.get().to_flags()
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_set_flags(raw_handle: *const Handle, flags: u32) {
-    let rhandle = &*raw_handle;
+pub unsafe extern "C" fn rsvg_handle_rust_set_flags(raw_handle: *const RsvgHandle, flags: u32) {
+    let rhandle = get_rust_handle(raw_handle);
 
     rhandle.load_flags.set(LoadFlags::from_flags(flags));
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_set_size_callback(
-    raw_handle: *mut Handle,
+    raw_handle: *mut RsvgHandle,
     size_func: RsvgSizeFunc,
     user_data: glib_sys::gpointer,
     destroy_notify: glib_sys::GDestroyNotify,
 ) {
-    let rhandle = &mut *raw_handle;
+    let rhandle = get_rust_handle(raw_handle);
 
     *rhandle.size_callback.borrow_mut() = SizeCallback {
         size_func,
@@ -700,10 +700,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_set_size_callback(
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_set_testing(
-    raw_handle: *const Handle,
+    raw_handle: *mut RsvgHandle,
     testing: glib_sys::gboolean,
 ) {
-    let rhandle = &*raw_handle;
+    let rhandle = get_rust_handle(raw_handle);
 
     rhandle.is_testing.set(from_glib(testing));
 }


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