[librsvg: 26/38] Finish porting the GObject boilerplate to Rust. Yay!!!!!!!
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 26/38] Finish porting the GObject boilerplate to Rust. Yay!!!!!!!
- Date: Fri, 25 Jan 2019 19:39:33 +0000 (UTC)
commit 3f47eeb92dc80987960a0b5ba28d340485633fba
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Jan 23 14:23:17 2019 -0600
Finish porting the GObject boilerplate to Rust. Yay!!!!!!!
librsvg/rsvg-handle.c | 240 +------------------------------------
rsvg_internals/src/c_api.rs | 27 ++++-
rsvg_internals/src/handle.rs | 24 ++--
rsvg_internals/src/pixbuf_utils.rs | 3 +-
4 files changed, 37 insertions(+), 257 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 0fb6beb1..f5281e93 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -197,236 +197,13 @@ extern RsvgHandle *rsvg_handle_rust_new_from_data (const guint8 *data,
gsize data_len,
GError **error);
-typedef struct {
- RsvgHandleRust *rust_handle;
-} RsvgHandlePrivate;
-
-enum {
- PROP_0,
- PROP_FLAGS,
- PROP_DPI_X,
- PROP_DPI_Y,
- PROP_BASE_URI,
- PROP_WIDTH,
- PROP_HEIGHT,
- PROP_EM,
- PROP_EX,
- PROP_TITLE,
- PROP_DESC,
- PROP_METADATA,
- NUM_PROPS
-};
-
-G_DEFINE_TYPE_WITH_CODE (RsvgHandle, rsvg_handle, G_TYPE_OBJECT,
- G_ADD_PRIVATE (RsvgHandle))
-
-static void
-rsvg_handle_init (RsvgHandle *self)
-{
- RsvgHandlePrivate *priv = rsvg_handle_get_instance_private (self);
- priv->rust_handle = rsvg_handle_rust_new();
-}
-
-static void
-rsvg_handle_dispose (GObject *instance)
-{
- RsvgHandle *self = (RsvgHandle *) instance;
- RsvgHandlePrivate *priv = rsvg_handle_get_instance_private (self);
-
- g_clear_pointer (&priv->rust_handle, rsvg_handle_rust_free);
-
- G_OBJECT_CLASS (rsvg_handle_parent_class)->dispose (instance);
-}
-
-static void
-rsvg_handle_set_property (GObject * instance, guint prop_id, GValue const *value, GParamSpec * pspec)
-{
- RsvgHandle *self = RSVG_HANDLE (instance);
-
- switch (prop_id) {
- case PROP_FLAGS:
- rsvg_handle_rust_set_flags (self, g_value_get_flags (value));
- break;
- case PROP_DPI_X:
- rsvg_handle_rust_set_dpi_x (self, g_value_get_double (value));
- break;
- case PROP_DPI_Y:
- 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);
-
- if (str) {
- rsvg_handle_set_base_uri (self, str);
- }
-
- break;
- }
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (instance, prop_id, pspec);
- }
-}
-
-static void
-rsvg_handle_get_property (GObject * instance, guint prop_id, GValue * value, GParamSpec * pspec)
-{
- RsvgHandle *self = RSVG_HANDLE (instance);
- RsvgDimensionData dim;
-
- switch (prop_id) {
- case PROP_FLAGS:
- 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 (self));
- break;
- case PROP_DPI_Y:
- 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));
- break;
- case PROP_WIDTH:
- rsvg_handle_get_dimensions (self, &dim);
- g_value_set_int (value, dim.width);
- break;
- case PROP_HEIGHT:
- rsvg_handle_get_dimensions (self, &dim);
- g_value_set_int (value, dim.height);
- break;
- case PROP_EM:
- rsvg_handle_get_dimensions (self, &dim);
- g_value_set_double (value, dim.em);
- break;
- case PROP_EX:
- rsvg_handle_get_dimensions (self, &dim);
- g_value_set_double (value, dim.ex);
- break;
- case PROP_TITLE:
- /* deprecated */
- break;
- case PROP_DESC:
- /* deprecated */
- break;
- case PROP_METADATA:
- g_value_set_string (value, rsvg_handle_get_metadata (self));
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (instance, prop_id, pspec);
- }
-}
+/* Implemented in rsvg_internals/src/c_api.rs */
+extern GType rsvg_handle_rust_get_type (void);
-static void
-rsvg_handle_class_init (RsvgHandleClass * klass)
+GType
+rsvg_handle_get_type (void)
{
- GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-
- gobject_class->dispose = rsvg_handle_dispose;
- gobject_class->set_property = rsvg_handle_set_property;
- gobject_class->get_property = rsvg_handle_get_property;
-
- /**
- * RsvgHandle:flags:
- *
- * Flags from #RsvgHandleFlags.
- *
- * Since: 2.36
- */
- g_object_class_install_property (gobject_class,
- PROP_FLAGS,
- g_param_spec_flags ("flags", NULL, NULL,
- RSVG_TYPE_HANDLE_FLAGS,
- RSVG_HANDLE_FLAGS_NONE,
- G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
- /**
- * dpi-x:
- */
- g_object_class_install_property (gobject_class,
- PROP_DPI_X,
- g_param_spec_double ("dpi-x", _("Horizontal resolution"),
- _("Horizontal resolution"),
- 0., G_MAXDOUBLE, 0.,
- (GParamFlags) (G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT)));
-
- g_object_class_install_property (gobject_class,
- PROP_DPI_Y,
- g_param_spec_double ("dpi-y", _("Vertical resolution"),
- _("Vertical resolution"),
- 0., G_MAXDOUBLE, 0.,
- (GParamFlags) (G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT)));
-
- g_object_class_install_property (gobject_class,
- PROP_BASE_URI,
- g_param_spec_string ("base-uri", _("Base URI"),
- _("Base URI"), NULL,
- (GParamFlags) (G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT)));
-
- g_object_class_install_property (gobject_class,
- PROP_WIDTH,
- g_param_spec_int ("width", _("Image width"),
- _("Image width"), 0, G_MAXINT, 0,
- (GParamFlags) (G_PARAM_READABLE)));
-
- g_object_class_install_property (gobject_class,
- PROP_HEIGHT,
- g_param_spec_int ("height", _("Image height"),
- _("Image height"), 0, G_MAXINT, 0,
- (GParamFlags) (G_PARAM_READABLE)));
-
- g_object_class_install_property (gobject_class,
- PROP_EM,
- g_param_spec_double ("em", _("em"),
- _("em"), 0, G_MAXDOUBLE, 0,
- (GParamFlags) (G_PARAM_READABLE)));
-
- g_object_class_install_property (gobject_class,
- PROP_EX,
- g_param_spec_double ("ex", _("ex"),
- _("ex"), 0, G_MAXDOUBLE, 0,
- (GParamFlags) (G_PARAM_READABLE)));
-
- /**
- * RsvgHandle:title:
- *
- * SVG's description
- *
- * Deprecated: 2.36
- */
- g_object_class_install_property (gobject_class,
- PROP_TITLE,
- g_param_spec_string ("title", _("Title"),
- _("SVG file title"), NULL,
- (GParamFlags) (G_PARAM_READABLE)));
-
- /**
- * RsvgHandle:desc:
- *
- * SVG's description
- *
- * Deprecated: 2.36
- */
- g_object_class_install_property (gobject_class,
- PROP_DESC,
- g_param_spec_string ("desc", _("Description"),
- _("SVG file description"), NULL,
- (GParamFlags) (G_PARAM_READABLE)));
-
- /**
- * RsvgHandle:metadata:
- *
- * SVG's description
- *
- * Deprecated: 2.36
- */
- g_object_class_install_property (gobject_class,
- PROP_METADATA,
- g_param_spec_string ("metadata", _("Metadata"),
- _("SVG file metadata"), NULL,
- (GParamFlags) (G_PARAM_READABLE)));
+ return rsvg_handle_rust_get_type ();
}
/**
@@ -796,13 +573,6 @@ rsvg_handle_get_desc (RsvgHandle * handle)
return NULL;
}
-RsvgHandleRust *
-rsvg_handle_get_rust (RsvgHandle *handle)
-{
- RsvgHandlePrivate *priv = rsvg_handle_get_instance_private (handle);
- return priv->rust_handle;
-}
-
/**
* rsvg_handle_render_cairo_sub:
* @handle: A #RsvgHandle
diff --git a/rsvg_internals/src/c_api.rs b/rsvg_internals/src/c_api.rs
index b4979c81..992338e9 100644
--- a/rsvg_internals/src/c_api.rs
+++ b/rsvg_internals/src/c_api.rs
@@ -243,7 +243,15 @@ impl ObjectImpl for Handle {
}
subclass::Property("base-uri", ..) => {
- self.set_base_url(value.get().expect("base-uri should be a non-NULL string"));
+ let v: Option<String> = value.get();;
+
+ // rsvg_handle_set_base_uri() expects non-NULL URI strings,
+ // but the "base-uri" property can be set to NULL due to a missing
+ // construct-time property.
+
+ if let Some(s) = v {
+ self.set_base_url(&s);
+ }
}
_ => unreachable!("invalid property id {}", id),
@@ -273,11 +281,22 @@ impl ObjectImpl for Handle {
subclass::Property("em", ..) => Ok(self.get_dimensions_no_error().em.to_value()),
subclass::Property("ex", ..) => Ok(self.get_dimensions_no_error().ex.to_value()),
- subclass::Property("title", ..) => Ok("".to_value()), // deprecated
- subclass::Property("desc", ..) => Ok("".to_value()), // deprecated
- subclass::Property("metadata", ..) => Ok("".to_value()), // deprecated
+ // the following three are deprecated
+ subclass::Property("title", ..) => Ok((None as Option<String>).to_value()),
+ subclass::Property("desc", ..) => Ok((None as Option<String>).to_value()),
+ subclass::Property("metadata", ..) => Ok((None as Option<String>).to_value()),
_ => unreachable!("invalid property id={} for RsvgHandle", id),
}
}
}
+
+pub fn get_rust_handle<'a>(handle: *const RsvgHandle) -> &'a Handle {
+ let handle = unsafe { &*handle };
+ handle.get_impl()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_handle_rust_get_type() -> glib_sys::GType {
+ Handle::get_type().to_glib()
+}
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index d7d14897..3492c366 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -13,6 +13,7 @@ use gdk_pixbuf::Pixbuf;
use gdk_pixbuf_sys;
use gio::{self, FileExt};
use gio_sys;
+use glib::subclass::types::ObjectSubclass;
use glib::translate::*;
use glib::{self, Bytes, Cast};
use glib_sys;
@@ -21,7 +22,7 @@ use libc;
use url::Url;
use allowed_url::{AllowedUrl, Href};
-use c_api::{HandleFlags, RsvgHandle, RsvgHandleFlags};
+use c_api::{get_rust_handle, HandleFlags, RsvgHandle, RsvgHandleFlags};
use dpi::Dpi;
use drawing_ctx::{DrawingCtx, RsvgRectangle};
use error::{set_gerror, DefsLookupErrorKind, LoadingError, RenderingError};
@@ -35,10 +36,10 @@ use xml::XmlState;
use xml2_load::xml_state_load_from_possibly_compressed_stream;
// A *const RsvgHandle is just an opaque pointer we get from C
-#[repr(C)]
-pub struct RsvgHandle {
- _private: [u8; 0],
-}
+// #[repr(C)]
+// pub struct RsvgHandle {
+// _private: [u8; 0],
+// }
// Keep in sync with rsvg.h:RsvgDimensionData
#[repr(C)]
@@ -656,13 +657,6 @@ impl LoadFlags {
}
}
-#[allow(improper_ctypes)]
-extern "C" {
- fn rsvg_handle_get_type() -> glib_sys::GType;
-
- fn rsvg_handle_get_rust(handle: *const RsvgHandle) -> *const Handle;
-}
-
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_new() -> *mut Handle {
Box::into_raw(Box::new(Handle::new()))
@@ -674,10 +668,6 @@ pub unsafe extern "C" fn rsvg_handle_rust_free(raw_handle: *mut Handle) {
Box::from_raw(raw_handle);
}
-pub fn get_rust_handle<'a>(handle: *const RsvgHandle) -> &'a Handle {
- unsafe { &*rsvg_handle_get_rust(handle) }
-}
-
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_set_base_url(
raw_handle: *const RsvgHandle,
@@ -1030,7 +1020,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_position_sub(
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_new_with_flags(flags: u32) -> *const RsvgHandle {
let obj: *mut gobject_sys::GObject =
- glib::Object::new(from_glib(rsvg_handle_get_type()), &[("flags", &flags)])
+ glib::Object::new(Handle::get_type(), &[("flags", &flags)])
.unwrap()
.to_glib_full();
diff --git a/rsvg_internals/src/pixbuf_utils.rs b/rsvg_internals/src/pixbuf_utils.rs
index 1324fa69..1bea47c9 100644
--- a/rsvg_internals/src/pixbuf_utils.rs
+++ b/rsvg_internals/src/pixbuf_utils.rs
@@ -7,8 +7,9 @@ use glib::translate::*;
use glib_sys;
use libc;
+use c_api::get_rust_handle;
use error::{set_gerror, RenderingError};
-use handle::{get_rust_handle, rsvg_handle_rust_new_from_gfile_sync, Handle, RsvgDimensionData};
+use handle::{rsvg_handle_rust_new_from_gfile_sync, Handle, RsvgDimensionData};
use rect::IRect;
use surface_utils::{
iterators::Pixels,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]