[librsvg: 9/14] Convert RSVG_ERROR_FAILED to the new genum proc-macro




commit ec1c0db71edf8200b995b21f733433a40fe6e96d
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Mar 23 11:48:40 2021 -0600

    Convert RSVG_ERROR_FAILED to the new genum proc-macro

 src/c_api/handle.rs | 50 +++++++++++---------------------------------------
 tests/api.c         | 24 ++++++++++++------------
 2 files changed, 23 insertions(+), 51 deletions(-)
---
diff --git a/src/c_api/handle.rs b/src/c_api/handle.rs
index 2aa9a372..e83941d8 100644
--- a/src/c_api/handle.rs
+++ b/src/c_api/handle.rs
@@ -30,7 +30,6 @@ use std::path::PathBuf;
 use std::ptr;
 use std::slice;
 use std::str;
-use std::sync::Once;
 use std::{f64, i32};
 
 use gdk_pixbuf::Pixbuf;
@@ -50,8 +49,6 @@ use glib::{
 
 use glib::types::instance_of;
 
-use gobject_sys::GEnumValue;
-
 use crate::api::{self, CairoRenderer, IntrinsicDimensions, Loader, LoadingError, SvgHandle};
 
 use crate::{
@@ -1105,38 +1102,7 @@ pub unsafe extern "C" fn rsvg_handle_get_type() -> glib_sys::GType {
 
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_error_get_type() -> glib_sys::GType {
-    static ONCE: Once = Once::new();
-    static mut ETYPE: glib_sys::GType = gobject_sys::G_TYPE_INVALID;
-
-    // We have to store the GEnumValue in a static variable but
-    // that requires it to be Sync. It is not Sync by default
-    // because it contains pointers, so we have define a custom
-    // wrapper type here on which we can implement Sync.
-    #[repr(transparent)]
-    struct GEnumValueWrapper(GEnumValue);
-    unsafe impl Sync for GEnumValueWrapper {}
-
-    static VALUES: [GEnumValueWrapper; 2] = [
-        GEnumValueWrapper(GEnumValue {
-            value: RSVG_ERROR_FAILED,
-            value_name: b"RSVG_ERROR_FAILED\0" as *const u8 as *const _,
-            value_nick: b"failed\0" as *const u8 as *const _,
-        }),
-        GEnumValueWrapper(GEnumValue {
-            value: 0,
-            value_name: 0 as *const _,
-            value_nick: 0 as *const _,
-        }),
-    ];
-
-    ONCE.call_once(|| {
-        ETYPE = gobject_sys::g_enum_register_static(
-            b"RsvgError\0" as *const u8 as *const _,
-            &VALUES as *const GEnumValueWrapper as *const GEnumValue,
-        );
-    });
-
-    ETYPE
+    Error::static_type().to_glib()
 }
 
 #[no_mangle]
@@ -2110,6 +2076,15 @@ pub(crate) fn set_gerror(err: *mut *mut glib_sys::GError, code: u32, msg: &str)
     }
 }
 
+#[derive(Debug, Eq, PartialEq, Clone, Copy, glib::GEnum)]
+#[repr(u32)]
+#[genum(type_name = "RsvgError")]
+enum Error {
+    #[genum(name = "RSVG_ERROR_FAILED", nick="failed")]
+    // Keep in sync with rsvg.h:RsvgError
+    Failed = 0,
+}
+
 /// Used as a generic error to translate to glib::Error
 ///
 /// This type implements `glib::error::ErrorDomain`, so it can be used
@@ -2119,16 +2094,13 @@ pub(crate) fn set_gerror(err: *mut *mut glib_sys::GError, code: u32, msg: &str)
 #[derive(Copy, Clone)]
 struct RsvgError;
 
-// Keep in sync with rsvg.h:RsvgError
-const RSVG_ERROR_FAILED: i32 = 0;
-
 impl ErrorDomain for RsvgError {
     fn domain() -> glib::Quark {
         glib::Quark::from_string("rsvg-error-quark")
     }
 
     fn code(self) -> i32 {
-        RSVG_ERROR_FAILED
+        Error::Failed as i32
     }
 
     fn from(_code: i32) -> Option<Self> {
diff --git a/tests/api.c b/tests/api.c
index 75b8b245..9c6e65c0 100644
--- a/tests/api.c
+++ b/tests/api.c
@@ -88,15 +88,15 @@ flags_registration (void)
     g_type_class_unref (type_class);
 }
 
-static gboolean
-enum_value_matches (GEnumValue *v,
-                    gint value,
-                    const char *value_name,
-                    const char *value_nick)
+static void
+assert_enum_value_matches (GEnumValue *v,
+                           gint value,
+                           const char *value_name,
+                           const char *value_nick)
 {
-    return (v->value == value
-            && strcmp (v->value_name, value_name) == 0
-            && strcmp (v->value_nick, value_nick) == 0);
+    g_assert_cmpint (v->value, ==, value);
+    g_assert_cmpstr (v->value_name, ==, value_name);
+    g_assert_cmpstr (v->value_nick, ==, value_nick);
 }
 
 static void
@@ -125,10 +125,10 @@ error_registration (void)
     enum_class = G_ENUM_CLASS (type_class);
     g_assert_cmpint (enum_class->n_values, ==, 1);
 
-    g_assert (enum_value_matches (&enum_class->values[0],
-                                  RSVG_ERROR_FAILED,
-                                  "RSVG_ERROR_FAILED",
-                                  "failed"));
+    assert_enum_value_matches (&enum_class->values[0],
+                               RSVG_ERROR_FAILED,
+                               "RSVG_ERROR_FAILED",
+                               "failed");
 
     g_type_class_unref (type_class);
 }


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