[librsvg/librsvg-2.44] 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/librsvg-2.44] Don't use zero-sized structs for FFI pointers where the structs originate in Rust
- Date: Thu, 6 Dec 2018 19:08:20 +0000 (UTC)
commit 9386e936fa4d9c480abfb1f72c44d9e2c4dd3654
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/css.rs | 14 ++++---------
rsvg_internals/src/defs.rs | 24 ++++++----------------
rsvg_internals/src/drawing_ctx.rs | 43 ++++++++++++++++-----------------------
rsvg_internals/src/handle.rs | 14 ++++++-------
rsvg_internals/src/load.rs | 6 +++---
rsvg_internals/src/tree.rs | 33 +++++++++++-------------------
6 files changed, 49 insertions(+), 85 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index c8f17e9c..d0b96270 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -15,11 +15,6 @@ use handle::{self, RsvgHandle};
use state::State;
use util::utf8_cstr;
-#[repr(C)]
-pub struct RsvgCssStyles {
- _private: [u8; 0],
-}
-
struct Declaration {
prop_value: String,
important: bool,
@@ -229,15 +224,14 @@ unsafe extern "C" fn css_unrecoverable_error(_a_this: *mut CRDocHandler) {
}
#[no_mangle]
-pub extern "C" fn rsvg_css_styles_new() -> *mut RsvgCssStyles {
- Box::into_raw(Box::new(CssStyles::new())) as *mut RsvgCssStyles
+pub extern "C" fn rsvg_css_styles_new() -> *mut CssStyles {
+ Box::into_raw(Box::new(CssStyles::new()))
}
#[no_mangle]
-pub unsafe extern "C" fn rsvg_css_styles_free(raw_styles: *mut RsvgCssStyles) {
+pub unsafe extern "C" fn rsvg_css_styles_free(raw_styles: *mut CssStyles) {
assert!(!raw_styles.is_null());
-
- Box::from_raw(raw_styles as *mut CssStyles);
+ Box::from_raw(raw_styles);
}
#[no_mangle]
diff --git a/rsvg_internals/src/defs.rs b/rsvg_internals/src/defs.rs
index d213a074..f9e901a1 100644
--- a/rsvg_internals/src/defs.rs
+++ b/rsvg_internals/src/defs.rs
@@ -8,11 +8,6 @@ use handle::{self, RsvgHandle};
use node::{Node, RsvgNode};
use util::utf8_cstr;
-#[repr(C)]
-pub struct RsvgDefs {
- _private: [u8; 0],
-}
-
pub struct Defs {
handle: *const RsvgHandle,
nodes: HashMap<String, Rc<Node>>,
@@ -65,29 +60,22 @@ impl Defs {
}
#[no_mangle]
-pub extern "C" fn rsvg_defs_new(handle: *const RsvgHandle) -> *mut RsvgDefs {
- Box::into_raw(Box::new(Defs::new(handle))) as *mut RsvgDefs
+pub extern "C" fn rsvg_defs_new(handle: *const RsvgHandle) -> *mut Defs {
+ Box::into_raw(Box::new(Defs::new(handle)))
}
#[no_mangle]
-pub extern "C" fn rsvg_defs_free(defs: *mut RsvgDefs) {
+pub unsafe extern "C" fn rsvg_defs_free(defs: *mut Defs) {
assert!(!defs.is_null());
-
- unsafe {
- let defs = { &mut *(defs as *mut Defs) };
- Box::from_raw(defs);
- }
+ Box::from_raw(defs);
}
#[no_mangle]
-pub extern "C" fn rsvg_defs_lookup(
- defs: *mut RsvgDefs,
- name: *const libc::c_char,
-) -> *const RsvgNode {
+pub extern "C" fn rsvg_defs_lookup(defs: *mut Defs, name: *const libc::c_char) -> *const RsvgNode {
assert!(!defs.is_null());
assert!(!name.is_null());
- let defs = unsafe { &mut *(defs as *mut Defs) };
+ let defs = unsafe { &mut *defs };
let name = unsafe { utf8_cstr(name) };
match defs.lookup(name) {
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index ea56c710..53eb29d3 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -14,7 +14,7 @@ use std::rc::{Rc, Weak};
use bbox::BoundingBox;
use clip_path::{ClipPathUnits, NodeClipPath};
use coord_units::CoordUnits;
-use defs::{Defs, RsvgDefs};
+use defs::Defs;
use error::RenderingError;
use filters;
use float_eq_cairo::ApproxEqCairo;
@@ -35,7 +35,7 @@ use state::{
StrokeLinejoin,
TextRendering,
};
-use tree::{RsvgTree, Tree};
+use tree::Tree;
use unitinterval::UnitInterval;
use viewbox::ViewBox;
@@ -96,11 +96,6 @@ impl Drop for ViewParams {
}
}
-#[repr(C)]
-pub struct RsvgDrawingCtx {
- _private: [u8; 0],
-}
-
pub struct DrawingCtx<'a> {
rect: cairo::Rectangle,
dpi_x: f64,
@@ -1073,14 +1068,14 @@ 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_tree: *const RsvgTree,
+ raw_draw_ctx: *mut DrawingCtx<'_>,
+ raw_tree: *const Tree,
) -> 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 };
assert!(!raw_tree.is_null());
- let tree = unsafe { &*(raw_tree as *const Tree) };
+ let tree = unsafe { &*raw_tree };
// 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
@@ -1097,11 +1092,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 };
@@ -1111,11 +1106,11 @@ pub extern "C" fn rsvg_drawing_ctx_add_node_and_ancestors_to_stack(
#[no_mangle]
pub extern "C" fn rsvg_drawing_ctx_get_ink_rect(
- raw_draw_ctx: *const RsvgDrawingCtx,
+ raw_draw_ctx: *const DrawingCtx<'_>,
ink_rect: *mut cairo_sys::cairo_rectangle_t,
) -> glib_sys::gboolean {
assert!(!raw_draw_ctx.is_null());
- let draw_ctx = unsafe { &mut *(raw_draw_ctx as *mut DrawingCtx<'_>) };
+ let draw_ctx = unsafe { &*raw_draw_ctx };
assert!(!ink_rect.is_null());
@@ -1171,7 +1166,7 @@ impl NodeStack {
}
#[no_mangle]
-pub extern "C" fn rsvg_drawing_ctx_new(
+pub extern "C" fn rsvg_drawing_ctx_new<'a>(
cr: *mut cairo_sys::cairo_t,
width: u32,
height: u32,
@@ -1179,11 +1174,11 @@ pub extern "C" fn rsvg_drawing_ctx_new(
vb_height: libc::c_double,
dpi_x: libc::c_double,
dpi_y: libc::c_double,
- defs: *mut RsvgDefs,
+ defs: *mut Defs,
is_testing: glib_sys::gboolean,
-) -> *mut RsvgDrawingCtx {
+) -> *mut DrawingCtx<'a> {
assert!(!defs.is_null());
- let defs = unsafe { &mut *(defs as *mut Defs) };
+ let defs = unsafe { &mut *defs };
Box::into_raw(Box::new(DrawingCtx::new(
unsafe { from_glib_none(cr) },
@@ -1195,15 +1190,11 @@ pub extern "C" fn rsvg_drawing_ctx_new(
dpi_y,
defs,
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 8ebb3162..64d63840 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -8,8 +8,8 @@ use glib::translate::*;
use glib_sys;
use libc;
-use css::{CssStyles, RsvgCssStyles};
-use defs::{Defs, RsvgDefs};
+use css::CssStyles;
+use defs::Defs;
use error::LoadingError;
use surface_utils::shared_surface::SharedImageSurface;
@@ -20,7 +20,7 @@ pub struct RsvgHandle {
#[allow(improper_ctypes)]
extern "C" {
- fn rsvg_handle_get_defs(handle: *const RsvgHandle) -> *const RsvgDefs;
+ fn rsvg_handle_get_defs(handle: *const RsvgHandle) -> *const Defs;
fn rsvg_handle_resolve_uri(
handle: *const RsvgHandle,
@@ -32,7 +32,7 @@ extern "C" {
uri: *const libc::c_char,
) -> *const RsvgHandle;
- fn rsvg_handle_get_css_styles(handle: *const RsvgHandle) -> *mut RsvgCssStyles;
+ fn rsvg_handle_get_css_styles(handle: *const RsvgHandle) -> *mut CssStyles;
fn _rsvg_handle_acquire_data(
handle: *mut RsvgHandle,
@@ -48,7 +48,7 @@ extern "C" {
pub fn get_defs<'a>(handle: *const RsvgHandle) -> &'a Defs {
unsafe {
let d = rsvg_handle_get_defs(handle);
- &*(d as *const Defs)
+ &*d
}
}
@@ -68,11 +68,11 @@ pub fn load_extern(handle: *const RsvgHandle, uri: &str) -> *const RsvgHandle {
}
pub fn get_css_styles<'a>(handle: *const RsvgHandle) -> &'a CssStyles {
- unsafe { &*(rsvg_handle_get_css_styles(handle) as *const CssStyles) }
+ unsafe { &*rsvg_handle_get_css_styles(handle) }
}
pub fn get_css_styles_mut<'a>(handle: *const RsvgHandle) -> &'a mut CssStyles {
- unsafe { &mut *(rsvg_handle_get_css_styles(handle) as *mut CssStyles) }
+ unsafe { &mut *rsvg_handle_get_css_styles(handle) }
}
pub struct BinaryData {
diff --git a/rsvg_internals/src/load.rs b/rsvg_internals/src/load.rs
index 53142970..797e5bec 100644
--- a/rsvg_internals/src/load.rs
+++ b/rsvg_internals/src/load.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
use attributes::Attribute;
use clip_path::NodeClipPath;
-use defs::{Defs, RsvgDefs};
+use defs::Defs;
use filters::{
blend::Blend,
color_matrix::ColorMatrix,
@@ -276,7 +276,7 @@ pub extern "C" fn rsvg_load_new_node(
raw_name: *const libc::c_char,
parent: *const RsvgNode,
pbag: *const PropertyBag<'_>,
- defs: *mut RsvgDefs,
+ defs: *mut Defs,
) -> *const RsvgNode {
assert!(!raw_name.is_null());
assert!(!pbag.is_null());
@@ -284,7 +284,7 @@ pub extern "C" fn rsvg_load_new_node(
let name = unsafe { utf8_cstr(raw_name) };
let pbag = unsafe { &*pbag };
- let defs = unsafe { &mut *(defs as *mut Defs) };
+ let defs = unsafe { &mut *defs };
let mut id = None;
let mut class = None;
diff --git a/rsvg_internals/src/tree.rs b/rsvg_internals/src/tree.rs
index 0148d6cc..9440b99a 100644
--- a/rsvg_internals/src/tree.rs
+++ b/rsvg_internals/src/tree.rs
@@ -7,11 +7,6 @@ use std::rc::Rc;
use node::{box_node, Node, NodeType, RsvgNode};
use state::ComputedValues;
-#[repr(C)]
-pub struct RsvgTree {
- _private: [u8; 0],
-}
-
pub struct Tree {
pub root: Rc<Node>,
already_cascaded: Cell<bool>,
@@ -39,44 +34,40 @@ impl Tree {
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_new(root: *const RsvgNode) -> *mut RsvgTree {
+pub extern "C" fn rsvg_tree_new(root: *const RsvgNode) -> *mut Tree {
assert!(!root.is_null());
let root: &RsvgNode = unsafe { &*root };
- Box::into_raw(Box::new(Tree::new(root))) as *mut RsvgTree
+ Box::into_raw(Box::new(Tree::new(root)))
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_free(tree: *mut RsvgTree) {
+pub unsafe extern "C" fn rsvg_tree_free(tree: *mut Tree) {
if !tree.is_null() {
- let tree = unsafe { &mut *(tree as *mut Tree) };
- let _ = unsafe { Box::from_raw(tree) };
+ Box::from_raw(tree);
}
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_cascade(tree: *const RsvgTree) {
+pub extern "C" fn rsvg_tree_cascade(tree: *const Tree) {
assert!(!tree.is_null());
- let tree = unsafe { &*(tree as *const Tree) };
+ let tree = unsafe { &*tree };
tree.cascade();
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_get_root(tree: *const RsvgTree) -> *mut RsvgNode {
+pub extern "C" fn rsvg_tree_get_root(tree: *const Tree) -> *mut RsvgNode {
assert!(!tree.is_null());
- let tree = unsafe { &*(tree as *const Tree) };
+ let tree = unsafe { &*tree };
box_node(tree.root.clone())
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_is_root(
- tree: *const RsvgTree,
- node: *mut RsvgNode,
-) -> glib_sys::gboolean {
+pub extern "C" fn rsvg_tree_is_root(tree: *const Tree, node: *mut RsvgNode) -> glib_sys::gboolean {
assert!(!tree.is_null());
- let tree = unsafe { &*(tree as *const Tree) };
+ let tree = unsafe { &*tree };
assert!(!node.is_null());
let node: &RsvgNode = unsafe { &*node };
@@ -85,9 +76,9 @@ pub extern "C" fn rsvg_tree_is_root(
}
#[no_mangle]
-pub extern "C" fn rsvg_tree_root_is_svg(tree: *const RsvgTree) -> glib_sys::gboolean {
+pub extern "C" fn rsvg_tree_root_is_svg(tree: *const Tree) -> glib_sys::gboolean {
assert!(!tree.is_null());
- let tree = unsafe { &*(tree as *const Tree) };
+ let tree = unsafe { &*tree };
tree.root_is_svg().to_glib()
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]