@@ -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"
@@ -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
@@ -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);
@@ -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");
}
// ------------------------------
@@ -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)
@@ -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);
@@ -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 {
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(-)