qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 12/14] rust: replace c"" literals with cstr crate


From: Paolo Bonzini
Subject: [PATCH 12/14] rust: replace c"" literals with cstr crate
Date: Mon, 1 Jul 2024 16:58:44 +0200

Part of what's needed to work with Rust versions prior to 1.77.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu/Cargo.toml            |  3 +++
 qemu/qom-rust.txt          |  2 +-
 qemu/src/hw/core/device.rs |  4 +++-
 qemu/src/qom/object.rs     |  4 +++-
 qemu/src/util/error.rs     |  4 +++-
 qemu/src/util/foreign.rs   | 20 +++++++++++---------
 qemu/tests/main.rs         |  7 ++++---
 8 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/qemu/Cargo.toml b/qemu/Cargo.toml
index 1100725..a07a449 100644
--- a/qemu/Cargo.toml
+++ b/qemu/Cargo.toml
@@ -7,5 +7,8 @@ edition = "2021"
 const-default = { version = "~1", features = ["derive"] }
 libc = "^0"
 
+# pick older version in order to support Rust 1.63
+cstr = { version = "=0.2.10" }
+
 [dev-dependencies]
 matches = ">=0"
diff --git a/qemu/qom-rust.txt b/qemu/qom-rust.txt
index 1588445..ef4bd06 100644
--- a/qemu/qom-rust.txt
+++ b/qemu/qom-rust.txt
@@ -48,7 +48,7 @@ Bindings for C classes
 struct must implement ObjectType
 
     unsafe impl ObjectType for Object {
-        const TYPE: &'static CStr = c"object";
+        const TYPE: &'static CStr = cstr!("object");
     }
 
 struct must implement IsA<T> for all superclasses T
diff --git a/qemu/src/hw/core/device.rs b/qemu/src/hw/core/device.rs
index 294251e..4edf61d 100644
--- a/qemu/src/hw/core/device.rs
+++ b/qemu/src/hw/core/device.rs
@@ -17,12 +17,14 @@ use crate::qom_isa;
 
 use crate::Result;
 
+use cstr::cstr;
+
 use std::ffi::CStr;
 use std::ops::Deref;
 use std::ptr::null_mut;
 
 unsafe impl ObjectType for DeviceState {
-    const TYPE: &'static CStr = c"device";
+    const TYPE: &'static CStr = cstr!("device");
 }
 
 qom_isa!(DeviceState, Object);
diff --git a/qemu/src/qom/object.rs b/qemu/src/qom/object.rs
index 4e84e29..9f6c078 100644
--- a/qemu/src/qom/object.rs
+++ b/qemu/src/qom/object.rs
@@ -7,6 +7,8 @@ use std::ffi::CStr;
 use std::fmt;
 use std::ops::Deref;
 
+use cstr::cstr;
+
 use crate::bindings::object_get_typename;
 use crate::bindings::object_property_add_child;
 use crate::bindings::object_new;
@@ -42,7 +44,7 @@ pub unsafe trait ObjectType: Sized {
 }
 
 unsafe impl ObjectType for Object {
-    const TYPE: &'static CStr = c"object";
+    const TYPE: &'static CStr = cstr!("object");
 }
 
 // ------------------------------
diff --git a/qemu/src/util/error.rs b/qemu/src/util/error.rs
index e7e6f2e..79c3c81 100644
--- a/qemu/src/util/error.rs
+++ b/qemu/src/util/error.rs
@@ -7,6 +7,8 @@ use crate::bindings::error_free;
 use crate::bindings::error_get_pretty;
 use crate::bindings::error_setg_internal;
 
+use cstr::cstr;
+
 use std::ffi::CStr;
 use std::fmt::{self, Display};
 use std::ptr;
@@ -215,7 +217,7 @@ impl CloneToForeign for Error {
                 ptr::null_mut(), // FIXME
                 0,
                 ptr::null_mut(), // FIXME
-                c"%s".as_ptr(),
+                cstr!("%s").as_ptr(),
                 format!("{}", self),
             );
             OwnedPointer::new(x)
diff --git a/qemu/src/util/foreign.rs b/qemu/src/util/foreign.rs
index 464400a..7a663cc 100644
--- a/qemu/src/util/foreign.rs
+++ b/qemu/src/util/foreign.rs
@@ -167,7 +167,8 @@ pub trait FromForeign: CloneToForeign + Sized {
     ///
     /// ```
     /// # use qemu::FromForeign;
-    /// let p = c"Hello, world!".as_ptr();
+    /// # use cstr::cstr;
+    /// let p = cstr!("Hello, world!").as_ptr();
     /// let s = unsafe {
     ///     String::cloned_from_foreign(p as *const libc::c_char)
     /// };
@@ -476,6 +477,7 @@ mod tests {
     #![allow(clippy::shadow_unrelated)]
 
     use super::*;
+    use cstr::cstr;
     use matches::assert_matches;
     use std::ffi::c_void;
 
@@ -498,7 +500,7 @@ mod tests {
     #[test]
     fn test_cloned_from_foreign_string_cow() {
         let s = "Hello, world!".to_string();
-        let cstr = c"Hello, world!";
+        let cstr = cstr!("Hello, world!");
         let cloned = unsafe { Cow::cloned_from_foreign(cstr.as_ptr()) };
         assert_eq!(s, cloned);
     }
@@ -506,7 +508,7 @@ mod tests {
     #[test]
     fn test_cloned_from_foreign_string() {
         let s = "Hello, world!".to_string();
-        let cstr = c"Hello, world!";
+        let cstr = cstr!("Hello, world!");
         let cloned = unsafe { String::cloned_from_foreign(cstr.as_ptr()) };
         assert_eq!(s, cloned);
     }
@@ -570,7 +572,7 @@ mod tests {
     #[test]
     fn test_clone_to_foreign_str() {
         let s = "Hello, world!";
-        let p = c"Hello, world!".as_ptr();
+        let p = cstr!("Hello, world!").as_ptr();
         let cloned = s.clone_to_foreign();
         unsafe {
             let len = libc::strlen(cloned.as_ptr());
@@ -588,7 +590,7 @@ mod tests {
 
     #[test]
     fn test_clone_to_foreign_cstr() {
-        let s: &CStr = c"Hello, world!";
+        let s: &CStr = cstr!("Hello, world!");
         let cloned = s.clone_to_foreign();
         unsafe {
             let len = libc::strlen(cloned.as_ptr());
@@ -606,7 +608,7 @@ mod tests {
 
     #[test]
     fn test_clone_to_foreign_string_cow() {
-        let p = c"Hello, world!".as_ptr();
+        let p = cstr!("Hello, world!").as_ptr();
         for s in vec![
             Into::<Cow<str>>::into("Hello, world!"),
             Into::<Cow<str>>::into("Hello, world!".to_string())] {
@@ -663,7 +665,7 @@ mod tests {
     #[test]
     fn test_clone_to_foreign_string() {
         let s = "Hello, world!".to_string();
-        let cstr = c"Hello, world!";
+        let cstr = cstr!("Hello, world!");
         let cloned = s.clone_to_foreign();
         unsafe {
             let len = libc::strlen(cloned.as_ptr());
@@ -683,7 +685,7 @@ mod tests {
     fn test_option() {
         // An Option can be used to produce or convert NULL pointers
         let s = Some("Hello, world!".to_string());
-        let cstr = c"Hello, world!";
+        let cstr = cstr!("Hello, world!");
         unsafe {
             assert_eq!(Option::<String>::cloned_from_foreign(cstr.as_ptr()), 
s);
             
assert_matches!(Option::<String>::cloned_from_foreign(ptr::null()), None);
@@ -695,7 +697,7 @@ mod tests {
     fn test_box() {
         // A box can be produced if the inner type has the capability.
         let s = Box::new("Hello, world!".to_string());
-        let cstr = c"Hello, world!";
+        let cstr = cstr!("Hello, world!");
         let cloned = unsafe { 
Box::<String>::cloned_from_foreign(cstr.as_ptr()) };
         assert_eq!(s, cloned);
 
diff --git a/qemu/tests/main.rs b/qemu/tests/main.rs
index e499c14..601e92b 100644
--- a/qemu/tests/main.rs
+++ b/qemu/tests/main.rs
@@ -1,4 +1,5 @@
 use const_default::ConstDefault;
+use cstr::cstr;
 
 use qemu::qom_define_type;
 use qemu::Object;
@@ -27,7 +28,7 @@ struct TestState {
 }
 
 qom_define_type!(
-    c"test-object",
+    cstr!("test-object"),
     TestObject,
     TestConf,
     ();
@@ -37,12 +38,12 @@ qom_define_type!(
 impl ObjectImpl for TestObject {}
 
 qdev_define_type!(
-    c"test-device",
+    cstr!("test-device"),
     TestDevice,
     TestConf,
     RefCell<TestState>;
     @extends DeviceState;
-    @properties [qdev_prop!(bool, c"foo", TestDevice, true, foo)]
+    @properties [qdev_prop!(bool, cstr!("foo"), TestDevice, true, foo)]
 );
 
 impl TestDevice {
-- 
2.45.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]