[librsvg] Don't use zero-sized structs for FFI pointers where the structs originate in Rust
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] Don't use zero-sized structs for FFI pointers where the structs originate in Rust
- Date: Thu, 6 Dec 2018 18:15:10 +0000 (UTC)
commit 8048e5a6830b0cb46847fb778ff9af83a9d85e65
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Dec 6 12:13:58 2018 -0600
Don't use zero-sized structs for FFI pointers where the structs originate in Rust
They are only needed for structs which originate in C. For Rust
structs which we export to C as opaque pointers, we can just take a
pointer to the struct without any casts.
rsvg_internals/src/drawing_ctx.rs | 29 ++++++++--------------
rsvg_internals/src/handle.rs | 51 +++++++++++++++++----------------------
rsvg_internals/src/xml.rs | 25 ++++++-------------
rsvg_internals/src/xml2_load.rs | 8 +++---
4 files changed, 44 insertions(+), 69 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 6cf74a74..0934a350 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -95,11 +95,6 @@ impl Drop for ViewParams {
}
}
-#[repr(C)]
-pub struct RsvgDrawingCtx {
- _private: [u8; 0],
-}
-
pub struct DrawingCtx {
handle: *const RsvgHandle,
@@ -1057,10 +1052,10 @@ impl From<TextRendering> for cairo::Antialias {
#[no_mangle]
pub extern "C" fn rsvg_drawing_ctx_draw_node_from_stack(
- raw_draw_ctx: *mut RsvgDrawingCtx,
+ raw_draw_ctx: *mut DrawingCtx,
) -> glib_sys::gboolean {
assert!(!raw_draw_ctx.is_null());
- let draw_ctx = unsafe { &mut *(raw_draw_ctx as *mut DrawingCtx) };
+ let draw_ctx = unsafe { &mut *raw_draw_ctx };
// FIXME: The public API doesn't let us return a GError from the rendering
// functions, just a boolean. Add a proper API to return proper errors from
@@ -1083,11 +1078,11 @@ pub extern "C" fn rsvg_drawing_ctx_draw_node_from_stack(
#[no_mangle]
pub extern "C" fn rsvg_drawing_ctx_add_node_and_ancestors_to_stack(
- raw_draw_ctx: *const RsvgDrawingCtx,
+ raw_draw_ctx: *mut DrawingCtx,
raw_node: *const RsvgNode,
) {
assert!(!raw_draw_ctx.is_null());
- let draw_ctx = unsafe { &mut *(raw_draw_ctx as *mut DrawingCtx) };
+ let draw_ctx = unsafe { &mut *raw_draw_ctx };
assert!(!raw_node.is_null());
let node = unsafe { &*raw_node };
@@ -1106,12 +1101,12 @@ pub struct RsvgRectangle {
#[no_mangle]
pub unsafe extern "C" fn rsvg_drawing_ctx_get_geometry(
- raw_draw_ctx: *const RsvgDrawingCtx,
+ raw_draw_ctx: *const DrawingCtx,
ink_rect: *mut RsvgRectangle,
logical_rect: *mut RsvgRectangle,
) {
assert!(!raw_draw_ctx.is_null());
- let draw_ctx = &mut *(raw_draw_ctx as *mut DrawingCtx);
+ let draw_ctx = &*raw_draw_ctx;
assert!(!ink_rect.is_null());
assert!(!logical_rect.is_null());
@@ -1198,7 +1193,7 @@ pub extern "C" fn rsvg_drawing_ctx_new(
dpi_x: libc::c_double,
dpi_y: libc::c_double,
is_testing: glib_sys::gboolean,
-) -> *mut RsvgDrawingCtx {
+) -> *mut DrawingCtx {
Box::into_raw(Box::new(DrawingCtx::new(
handle,
unsafe { from_glib_none(cr) },
@@ -1209,15 +1204,11 @@ pub extern "C" fn rsvg_drawing_ctx_new(
dpi_x,
dpi_y,
from_glib(is_testing),
- ))) as *mut RsvgDrawingCtx
+ )))
}
#[no_mangle]
-pub extern "C" fn rsvg_drawing_ctx_free(raw_draw_ctx: *mut RsvgDrawingCtx) {
+pub unsafe extern "C" fn rsvg_drawing_ctx_free(raw_draw_ctx: *mut DrawingCtx) {
assert!(!raw_draw_ctx.is_null());
- let draw_ctx = unsafe { &mut *(raw_draw_ctx as *mut DrawingCtx) };
-
- unsafe {
- Box::from_raw(draw_ctx);
- }
+ Box::from_raw(raw_draw_ctx);
}
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 967b8bec..8bcf92cb 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -22,19 +22,15 @@ use node::{box_node, Node, RsvgNode};
use surface_utils::shared_surface::SharedImageSurface;
use svg::Svg;
use util::rsvg_g_warning;
-use xml::{RsvgXmlState, XmlState};
+use xml::XmlState;
+// A *const RsvgHandle is just an opaque pointer we get from C
#[repr(C)]
pub struct RsvgHandle {
_private: [u8; 0],
}
-#[repr(C)]
-pub struct RsvgHandleRust {
- _private: [u8; 0],
-}
-
-struct Handle {
+pub struct Handle {
base_url: RefCell<Option<Url>>,
svg: RefCell<Option<Svg>>,
}
@@ -66,7 +62,7 @@ extern "C" {
href: *const libc::c_char,
) -> glib_sys::gboolean;
- fn rsvg_handle_get_rust(handle: *const RsvgHandle) -> *mut RsvgHandleRust;
+ fn rsvg_handle_get_rust(handle: *const RsvgHandle) -> *mut Handle;
}
pub fn lookup_node(handle: *const RsvgHandle, fragment: &Fragment) -> Option<Rc<Node>> {
@@ -261,15 +257,14 @@ pub fn get_svg<'a>(handle: *const RsvgHandle) -> Ref<'a, Option<Svg>> {
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_new() -> *mut RsvgHandleRust {
- Box::into_raw(Box::new(Handle::new())) as *mut RsvgHandleRust
+pub unsafe extern "C" fn rsvg_handle_rust_new() -> *mut Handle {
+ Box::into_raw(Box::new(Handle::new()))
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_free(raw_handle: *mut RsvgHandleRust) {
+pub unsafe extern "C" fn rsvg_handle_rust_free(raw_handle: *mut Handle) {
assert!(!raw_handle.is_null());
-
- Box::from_raw(raw_handle as *mut Handle);
+ Box::from_raw(raw_handle);
}
fn get_rust_handle<'a>(handle: *const RsvgHandle) -> &'a mut Handle {
@@ -278,10 +273,10 @@ 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 RsvgHandleRust,
+ raw_handle: *const Handle,
uri: *const libc::c_char,
) {
- let handle = &*(raw_handle as *const Handle);
+ let handle = &*raw_handle;
assert!(!uri.is_null());
let uri: String = from_glib_none(uri);
@@ -305,9 +300,9 @@ pub unsafe extern "C" fn rsvg_handle_rust_set_base_url(
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_get_base_gfile(
- raw_handle: *const RsvgHandleRust,
+ raw_handle: *const Handle,
) -> *mut gio_sys::GFile {
- let handle = &*(raw_handle as *const Handle);
+ let handle = &*raw_handle;
match *handle.base_url.borrow() {
None => ptr::null_mut(),
@@ -449,18 +444,18 @@ pub unsafe extern "C" fn rsvg_handle_acquire_stream(
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_steal_result(
- raw_handle: *const RsvgHandleRust,
- raw_xml_state: *mut RsvgXmlState,
+ raw_handle: *const Handle,
+ raw_xml_state: *mut XmlState,
) {
- let handle = &*(raw_handle as *const Handle);
- let xml = &mut *(raw_xml_state as *mut XmlState);
+ let handle = &*raw_handle;
+ let xml = &mut *raw_xml_state;
*handle.svg.borrow_mut() = Some(xml.steal_result());
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_cascade(raw_handle: *const RsvgHandleRust) {
- let rhandle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_cascade(raw_handle: *const Handle) {
+ let rhandle = &*raw_handle;
let svg_ref = rhandle.svg.borrow();
let svg = svg_ref.as_ref().unwrap();
@@ -469,10 +464,8 @@ pub unsafe extern "C" fn rsvg_handle_rust_cascade(raw_handle: *const RsvgHandleR
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_handle_rust_get_root(
- raw_handle: *const RsvgHandleRust,
-) -> *const RsvgNode {
- let rhandle = &*(raw_handle as *const Handle);
+pub unsafe extern "C" fn rsvg_handle_rust_get_root(raw_handle: *const Handle) -> *const RsvgNode {
+ let rhandle = &*raw_handle;
let svg_ref = rhandle.svg.borrow();
let svg = svg_ref.as_ref().unwrap();
@@ -482,10 +475,10 @@ pub unsafe extern "C" fn rsvg_handle_rust_get_root(
#[no_mangle]
pub unsafe extern "C" fn rsvg_handle_rust_node_is_root(
- raw_handle: *const RsvgHandleRust,
+ raw_handle: *const Handle,
node: *mut RsvgNode,
) -> glib_sys::gboolean {
- let rhandle = &*(raw_handle as *const Handle);
+ let rhandle = &*raw_handle;
assert!(!node.is_null());
let node: &RsvgNode = &*node;
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 30f2c6e1..d4036247 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -72,12 +72,6 @@ impl Context {
}
}
-// A *const RsvgXmlState is just the type that we export to C
-#[repr(C)]
-pub struct RsvgXmlState {
- _private: [u8; 0],
-}
-
// This is to hold an xmlEntityPtr from libxml2; we just hold an opaque pointer
// that is freed in impl Drop for XmlState
type XmlEntityPtr = *mut libc::c_void;
@@ -582,23 +576,20 @@ fn parse_xml_stylesheet_processing_instruction(data: &str) -> Result<Vec<(String
}
#[no_mangle]
-pub extern "C" fn rsvg_xml_state_new(handle: *mut RsvgHandle) -> *mut RsvgXmlState {
- Box::into_raw(Box::new(XmlState::new(handle))) as *mut RsvgXmlState
+pub extern "C" fn rsvg_xml_state_new(handle: *mut RsvgHandle) -> *mut XmlState {
+ Box::into_raw(Box::new(XmlState::new(handle)))
}
#[no_mangle]
-pub extern "C" fn rsvg_xml_state_free(xml: *mut RsvgXmlState) {
+pub unsafe extern "C" fn rsvg_xml_state_free(xml: *mut XmlState) {
assert!(!xml.is_null());
- let xml = unsafe { &mut *(xml as *mut XmlState) };
- unsafe {
- Box::from_raw(xml);
- }
+ Box::from_raw(xml);
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_xml_state_error(xml: *mut RsvgXmlState, msg: *const libc::c_char) {
+pub unsafe extern "C" fn rsvg_xml_state_error(xml: *mut XmlState, msg: *const libc::c_char) {
assert!(!xml.is_null());
- let xml = &mut *(xml as *mut XmlState);
+ let xml = &mut *xml;
assert!(!msg.is_null());
// Unlike the functions that take UTF-8 validated strings from
@@ -610,11 +601,11 @@ pub unsafe extern "C" fn rsvg_xml_state_error(xml: *mut RsvgXmlState, msg: *cons
#[no_mangle]
pub unsafe extern "C" fn rsvg_xml_state_tree_is_valid(
- xml: *mut RsvgXmlState,
+ xml: *mut XmlState,
error: *mut *mut glib_sys::GError,
) -> glib_sys::gboolean {
assert!(!xml.is_null());
- let xml = &mut *(xml as *mut XmlState);
+ let xml = &mut *xml;
if let Some(ref tree) = xml.tree {
if tree.root_is_svg() {
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index 4160291b..5cc39fd8 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -15,7 +15,7 @@ use glib::translate::*;
use error::{set_gerror, RsvgError};
use property_bag::PropertyBag;
use util::utf8_cstr;
-use xml::{RsvgXmlState, XmlState};
+use xml::XmlState;
use xml2::*;
extern "C" {
@@ -294,7 +294,7 @@ fn create_xml_stream_parser(
#[no_mangle]
pub unsafe extern "C" fn rsvg_create_xml_push_parser(
- xml: *mut RsvgXmlState,
+ xml: *mut XmlState,
unlimited_size: glib_sys::gboolean,
base_uri: *const libc::c_char,
error: *mut *mut glib_sys::GError,
@@ -347,14 +347,14 @@ pub unsafe extern "C" fn rsvg_set_error_from_xml(
#[no_mangle]
pub unsafe extern "C" fn rsvg_xml_state_parse_from_stream(
- xml: *mut RsvgXmlState,
+ xml: *mut XmlState,
unlimited_size: glib_sys::gboolean,
stream: *mut gio_sys::GInputStream,
cancellable: *mut gio_sys::GCancellable,
error: *mut *mut glib_sys::GError,
) -> glib_sys::gboolean {
assert!(!xml.is_null());
- let xml = &mut *(xml as *mut XmlState);
+ let xml = &mut *xml;
let unlimited_size = from_glib(unlimited_size);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]