diff mbox series

[PoCv2,11/15] qga: add qmp! macro helper

Message ID 20201011203513.1621355-12-marcandre.lureau@redhat.com
State New
Headers show
Series Rust binding for QAPI (qemu-ga only, for now) | expand

Commit Message

Marc-André Lureau Oct. 11, 2020, 8:35 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Add a macro to help wrapping higher-level qmp handlers, by taking care
of errors and return value pointer translation.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 qga/lib.rs     |  1 +
 qga/qmp/mod.rs | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 qga/qmp/mod.rs
diff mbox series

Patch

diff --git a/qga/lib.rs b/qga/lib.rs
index bff4107569..5fe08c25a3 100644
--- a/qga/lib.rs
+++ b/qga/lib.rs
@@ -1,2 +1,3 @@ 
 mod qapi;
 mod qapi_sys;
+mod qmp;
diff --git a/qga/qmp/mod.rs b/qga/qmp/mod.rs
new file mode 100644
index 0000000000..38060100af
--- /dev/null
+++ b/qga/qmp/mod.rs
@@ -0,0 +1,36 @@ 
+use common::*;
+
+use crate::*;
+
+macro_rules! qmp {
+    // the basic return value variant
+    ($e:expr, $errp:ident, $errval:expr) => {{
+        assert!(!$errp.is_null());
+        unsafe {
+            *$errp = std::ptr::null_mut();
+        }
+
+        match $e {
+            Ok(val) => val,
+            Err(err) => unsafe {
+                *$errp = err.to_qemu_full();
+                $errval
+            },
+        }
+    }};
+    // the ptr return value variant
+    ($e:expr, $errp:ident) => {{
+        assert!(!$errp.is_null());
+        unsafe {
+            *$errp = std::ptr::null_mut();
+        }
+
+        match $e {
+            Ok(val) => val.to_qemu_full().into(),
+            Err(err) => unsafe {
+                *$errp = err.to_qemu_full();
+                std::ptr::null_mut()
+            },
+        }
+    }};
+}