[PATCH 07/16] rust: use std::os::raw instead of core::ffi

Paolo Bonzini posted 16 patches 1 week ago
There is a newer version of this series
[PATCH 07/16] rust: use std::os::raw instead of core::ffi
Posted by Paolo Bonzini 1 week ago
core::ffi::c_* types were introduced in Rust 1.64.0.  Use the older types
in std::os::raw, which are now aliases of the types in core::ffi.  There is
no need to compile QEMU as no_std, so this is acceptable as long as we support
a version of Debian with Rust 1.63.0.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 meson.build                          |  3 +--
 rust/hw/char/pl011/src/device.rs     | 20 +++++++++++---------
 rust/hw/char/pl011/src/lib.rs        |  2 +-
 rust/hw/char/pl011/src/memory_ops.rs | 10 ++++++----
 rust/qemu-api/src/definitions.rs     |  4 +++-
 rust/qemu-api/src/device_class.rs    |  8 ++++----
 rust/qemu-api/src/lib.rs             |  8 +++++---
 7 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/meson.build b/meson.build
index 2545185014e..175b8d82228 100644
--- a/meson.build
+++ b/meson.build
@@ -3923,14 +3923,13 @@ if have_rust and have_system
   bindgen_args = [
     '--disable-header-comment',
     '--raw-line', '// @generated',
-    '--ctypes-prefix', 'core::ffi',
+    '--ctypes-prefix', 'std::os::raw',
     '--formatter', 'rustfmt',
     '--generate-block',
     '--generate-cstr',
     '--impl-debug',
     '--merge-extern-blocks',
     '--no-doc-comments',
-    '--use-core',
     '--with-derive-default',
     '--no-size_t-is-usize',
     '--no-layout-tests',
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index c7193b41bee..cd4c01c2336 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -2,9 +2,11 @@
 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
 // SPDX-License-Identifier: GPL-2.0-or-later
 
-use core::{
-    ffi::{c_int, c_uchar, c_uint, c_void, CStr},
-    ptr::{addr_of, addr_of_mut, NonNull},
+use core::ptr::{addr_of, addr_of_mut, NonNull};
+
+use std::{
+    ffi::CStr,
+    os::raw::{c_int, c_uchar, c_uint, c_void}
 };
 
 use qemu_api::{
@@ -89,10 +91,10 @@ pub struct PL011Class {
 
 impl qemu_api::definitions::Class for PL011Class {
     const CLASS_INIT: Option<
-        unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
+        unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
     > = Some(crate::device_class::pl011_class_init);
     const CLASS_BASE_INIT: Option<
-        unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
+        unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
     > = None;
 }
 
@@ -151,7 +153,7 @@ pub unsafe fn init(&mut self) {
     pub fn read(
         &mut self,
         offset: hwaddr,
-        _size: core::ffi::c_uint,
+        _size: c_uint,
     ) -> std::ops::ControlFlow<u64, u64> {
         use RegisterOffset::*;
 
@@ -532,9 +534,9 @@ pub fn update(&self) {
 /// The buffer and size arguments must also be valid.
 #[no_mangle]
 pub unsafe extern "C" fn pl011_receive(
-    opaque: *mut core::ffi::c_void,
+    opaque: *mut c_void,
     buf: *const u8,
-    size: core::ffi::c_int,
+    size: c_int,
 ) {
     unsafe {
         debug_assert!(!opaque.is_null());
@@ -555,7 +557,7 @@ pub fn update(&self) {
 /// the same size as [`PL011State`]. We also expect the device is
 /// readable/writeable from one thread at any time.
 #[no_mangle]
-pub unsafe extern "C" fn pl011_event(opaque: *mut core::ffi::c_void, event: QEMUChrEvent) {
+pub unsafe extern "C" fn pl011_event(opaque: *mut c_void, event: QEMUChrEvent) {
     unsafe {
         debug_assert!(!opaque.is_null());
         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
diff --git a/rust/hw/char/pl011/src/lib.rs b/rust/hw/char/pl011/src/lib.rs
index 2939ee50c99..2b157868b0f 100644
--- a/rust/hw/char/pl011/src/lib.rs
+++ b/rust/hw/char/pl011/src/lib.rs
@@ -45,7 +45,7 @@
 pub mod device_class;
 pub mod memory_ops;
 
-pub const TYPE_PL011: &::core::ffi::CStr = c"pl011";
+pub const TYPE_PL011: &::std::ffi::CStr = c"pl011";
 
 /// Offset of each register from the base memory address of the device.
 ///
diff --git a/rust/hw/char/pl011/src/memory_ops.rs b/rust/hw/char/pl011/src/memory_ops.rs
index 8d066ebf6d0..2c664fd45ed 100644
--- a/rust/hw/char/pl011/src/memory_ops.rs
+++ b/rust/hw/char/pl011/src/memory_ops.rs
@@ -4,6 +4,8 @@
 
 use core::{mem::MaybeUninit, ptr::NonNull};
 
+use std::os::raw::{c_uint, c_void};
+
 use qemu_api::bindings::*;
 
 use crate::device::PL011State;
@@ -24,9 +26,9 @@
 
 #[no_mangle]
 unsafe extern "C" fn pl011_read(
-    opaque: *mut core::ffi::c_void,
+    opaque: *mut c_void,
     addr: hwaddr,
-    size: core::ffi::c_uint,
+    size: c_uint,
 ) -> u64 {
     assert!(!opaque.is_null());
     let mut state = unsafe { NonNull::new_unchecked(opaque.cast::<PL011State>()) };
@@ -46,10 +48,10 @@
 
 #[no_mangle]
 unsafe extern "C" fn pl011_write(
-    opaque: *mut core::ffi::c_void,
+    opaque: *mut c_void,
     addr: hwaddr,
     data: u64,
-    _size: core::ffi::c_uint,
+    _size: c_uint,
 ) {
     unsafe {
         assert!(!opaque.is_null());
diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitions.rs
index 60bd3f8aaa6..aa7cdd69c99 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -4,7 +4,9 @@
 
 //! Definitions required by QEMU when registering a device.
 
-use ::core::ffi::{c_void, CStr};
+use std::ffi::CStr;
+
+use std::os::raw::c_void;
 
 use crate::bindings::{Object, ObjectClass, TypeInfo};
 
diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/device_class.rs
index 87892b50c63..871063d4a92 100644
--- a/rust/qemu-api/src/device_class.rs
+++ b/rust/qemu-api/src/device_class.rs
@@ -10,7 +10,7 @@ macro_rules! device_class_init {
         #[no_mangle]
         pub unsafe extern "C" fn $func(
             klass: *mut $crate::bindings::ObjectClass,
-            _: *mut ::core::ffi::c_void,
+            _: *mut ::std::os::raw::c_void,
         ) {
             let mut dc =
                 ::core::ptr::NonNull::new(klass.cast::<$crate::bindings::DeviceClass>()).unwrap();
@@ -30,7 +30,7 @@ macro_rules! define_property {
         $crate::bindings::Property {
             name: {
                 #[used]
-                static _TEMP: &::core::ffi::CStr = $name;
+                static _TEMP: &::std::ffi::CStr = $name;
                 _TEMP.as_ptr()
             },
             info: $prop,
@@ -51,7 +51,7 @@ macro_rules! define_property {
         $crate::bindings::Property {
             name: {
                 #[used]
-                static _TEMP: &::core::ffi::CStr = $name;
+                static _TEMP: &::std::ffi::CStr = $name;
                 _TEMP.as_ptr()
             },
             info: $prop,
@@ -121,7 +121,7 @@ macro_rules! vm_state_description {
         pub static $name: $crate::bindings::VMStateDescription = $crate::bindings::VMStateDescription {
             $(name: {
                 #[used]
-                static VMSTATE_NAME: &::core::ffi::CStr = $vname;
+                static VMSTATE_NAME: &::std::ffi::CStr = $vname;
                 $vname.as_ptr()
             },)*
             unmigratable: true,
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index e72fb4b4bb1..c2f60ac4727 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -35,6 +35,8 @@ unsafe impl Sync for bindings::VMStateDescription {}
 
 use std::alloc::{GlobalAlloc, Layout};
 
+use std::os::raw::c_void;
+
 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
 extern "C" {
     fn g_aligned_alloc0(
@@ -47,8 +49,8 @@ fn g_aligned_alloc0(
 
 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))]
 extern "C" {
-    fn qemu_memalign(alignment: usize, size: usize) -> *mut ::core::ffi::c_void;
-    fn qemu_vfree(ptr: *mut ::core::ffi::c_void);
+    fn qemu_memalign(alignment: usize, size: usize) -> *mut c_void;
+    fn qemu_vfree(ptr: *mut c_void);
 }
 
 extern "C" {
@@ -113,7 +115,7 @@ fn default() -> Self {
 }
 
 // Sanity check.
-const _: [(); 8] = [(); ::core::mem::size_of::<*mut ::core::ffi::c_void>()];
+const _: [(); 8] = [(); ::core::mem::size_of::<*mut c_void>()];
 
 unsafe impl GlobalAlloc for QemuAllocator {
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
-- 
2.46.2
Re: [PATCH 07/16] rust: use std::os::raw instead of core::ffi
Posted by Zhao Liu 4 days, 6 hours ago
On Tue, Oct 15, 2024 at 03:17:25PM +0200, Paolo Bonzini wrote:
> Date: Tue, 15 Oct 2024 15:17:25 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 07/16] rust: use std::os::raw instead of core::ffi
> X-Mailer: git-send-email 2.46.2
> 
> core::ffi::c_* types were introduced in Rust 1.64.0.  Use the older types
> in std::os::raw, which are now aliases of the types in core::ffi.  There is
> no need to compile QEMU as no_std, so this is acceptable as long as we support
> a version of Debian with Rust 1.63.0.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  meson.build                          |  3 +--
>  rust/hw/char/pl011/src/device.rs     | 20 +++++++++++---------
>  rust/hw/char/pl011/src/lib.rs        |  2 +-
>  rust/hw/char/pl011/src/memory_ops.rs | 10 ++++++----
>  rust/qemu-api/src/definitions.rs     |  4 +++-
>  rust/qemu-api/src/device_class.rs    |  8 ++++----
>  rust/qemu-api/src/lib.rs             |  8 +++++---
>  7 files changed, 31 insertions(+), 24 deletions(-)
> 

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>