[librsvg/wip/rust-api: 9/11] Turn most g_warnings into panics, and don't use g_warning() when built as a standalone Rust crate
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg/wip/rust-api: 9/11] Turn most g_warnings into panics, and don't use g_warning() when built as a standalone Rust crate
- Date: Sat, 16 Feb 2019 01:35:10 +0000 (UTC)
commit 4df0ef3d6dd8c277ef484b33104e9666ea6ea38c
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Feb 13 19:34:02 2019 -0600
Turn most g_warnings into panics, and don't use g_warning() when built as a standalone Rust crate
Most of the calls to rsvg_g_warning() indicate programming errors,
mostly around functions being called at the wrong time (e.g. the
is_loaded() checks). Those are panics now.
For the other genuine warnings, we cannot call rsvg_g_warning_from_c()
when building librsvg_crate, since that does not link to the C part of
librsvg.so. However, for the cases where that is used, those cases
do return meaningful Rust error codes, so they don't need warnings.
So, the rsvg_internals crate now has a feature "c-library" - when
enabled, it will call rsvg_g_warning_from_c(); when disabled, it will
do nothing.
Makefile.am | 2 +-
rsvg_internals/Cargo.toml | 4 ++++
rsvg_internals/src/handle.rs | 24 +++++++-----------------
rsvg_internals/src/util.rs | 18 ++++++++++++++----
4 files changed, 26 insertions(+), 22 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index fed1e52b..1001b3c2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -171,7 +171,7 @@ $(RUST_LIB): $(RUST_SRC)
PKG_CONFIG_ALLOW_CROSS=1 \
PKG_CONFIG='$(PKG_CONFIG)' \
CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) \
- $(CARGO) --locked build $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS)
+ $(CARGO) --locked build $(CARGO_VERBOSE) $(CARGO_TARGET_ARGS) $(CARGO_RELEASE_ARGS) --features
"c-library"
librsvg_@RSVG_API_MAJOR_VERSION@_la_CPPFLAGS = \
-I$(top_srcdir) \
diff --git a/rsvg_internals/Cargo.toml b/rsvg_internals/Cargo.toml
index 268498b9..f682338b 100644
--- a/rsvg_internals/Cargo.toml
+++ b/rsvg_internals/Cargo.toml
@@ -77,3 +77,7 @@ harness = false
[[bench]]
name = "srgb"
harness = false
+
+[features]
+# Enables calling g_warning() when built as part of librsvg.so
+c-library = []
\ No newline at end of file
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 94a005ac..1e9af90c 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -180,10 +180,7 @@ impl Handle {
fn set_base_url(&self, url: &str) {
if self.load_state.get() != LoadState::Start {
- rsvg_g_warning(
- "Please set the base file or URI before loading any data into RsvgHandle",
- );
- return;
+ panic!("Please set the base file or URI before loading any data into RsvgHandle",);
}
match Url::parse(&url) {
@@ -209,7 +206,7 @@ impl Handle {
if let Some(uri) = file.get_uri() {
self.set_base_url(&uri);
} else {
- rsvg_g_warning("file has no URI; will not set the base URI");
+ panic!("file has no URI; will not set the base URI");
}
}
@@ -711,23 +708,20 @@ pub unsafe extern "C" fn rsvg_handle_rust_set_testing(
fn is_loaded(handle: &Handle) -> bool {
match handle.load_state.get() {
LoadState::Start => {
- rsvg_g_warning("RsvgHandle has not been loaded");
- false
+ panic!("RsvgHandle has not been loaded");
}
LoadState::Loading => {
- rsvg_g_warning("RsvgHandle is still loading; call rsvg_handle_close() first");
- false
+ panic!("RsvgHandle is still loading; call rsvg_handle_close() first");
}
LoadState::ClosedOk => true,
LoadState::ClosedError => {
- rsvg_g_warning(
+ panic!(
"RsvgHandle could not read or parse the SVG; did you check for errors during the \
loading stage?",
);
- false
}
}
}
@@ -742,10 +736,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_read_stream_sync(
let rhandle = get_rust_handle(handle);
if rhandle.load_state.get() != LoadState::Start {
- rsvg_g_warning(
- "handle must not be already loaded in order to call rsvg_handle_read_stream_sync()",
- );
- return false.to_glib();
+ panic!("handle must not be already loaded in order to call rsvg_handle_read_stream_sync()",);
}
let stream = from_glib_none(stream);
@@ -772,8 +763,7 @@ pub unsafe extern "C" fn rsvg_handle_rust_write(
let load_state = rhandle.load_state.get();
if !(load_state == LoadState::Start || load_state == LoadState::Loading) {
- rsvg_g_warning("handle must not be closed in order to write to it");
- return;
+ panic!("handle must not be closed in order to write to it");
}
let buffer = slice::from_raw_parts(buf, count);
diff --git a/rsvg_internals/src/util.rs b/rsvg_internals/src/util.rs
index 3a7c7cac..b63387c9 100644
--- a/rsvg_internals/src/util.rs
+++ b/rsvg_internals/src/util.rs
@@ -3,6 +3,7 @@ use libc;
use std::ffi::CStr;
use std::str;
+#[cfg(feature = "c-library")]
use glib::translate::*;
/// Converts a `char *` which is known to be valid UTF-8 into a `&str`
@@ -27,12 +28,21 @@ pub fn clamp<T: PartialOrd>(val: T, low: T, high: T) -> T {
}
}
-extern "C" {
- fn rsvg_g_warning_from_c(msg: *const libc::c_char);
-}
-
+#[cfg(feature = "c-library")]
pub fn rsvg_g_warning(msg: &str) {
unsafe {
+ extern "C" {
+ fn rsvg_g_warning_from_c(msg: *const libc::c_char);
+ }
+
rsvg_g_warning_from_c(msg.to_glib_none().0);
}
}
+
+#[cfg(not(feature = "c-library"))]
+pub fn rsvg_g_warning(_msg: &str) {
+ // The only callers of this are in handle.rs. When those functions
+ // are called from the Rust API, they are able to return a
+ // meaningful error code, but the C API isn't - so they issues a
+ // g_warning() instead.
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]