* [PATCH 0/9] rust: allow minimum version of 1.77
@ 2025-05-02 10:23 Paolo Bonzini
2025-05-02 10:23 ` [PATCH 1/9] lcitool: use Rust 1.78 for Debian bookworm Paolo Bonzini
` (9 more replies)
0 siblings, 10 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Debian bookworm provides Rust 1.78 on all architectures except for mips64el,
as part of its packaging of Firefox. Michael Tokarev confirmed that
it is not a problem at this point to require a rustup-based toolchain
when building for mips64el on bookworm.
This series therefore updates the minimum supported Rust version
to 1.77 (since 1.78 does not really add anything that QEMU needs).
It removes several temporary compatibility shims (including offset_of)
and enables the introduction of some more modern idioms, for example
cast_const()/cast_mut() and "let ... else". These were useful as early
experiments with procedural macros and with supporting old versions of
Rust, but are not needed anymore.
The remaining major obstacle is const_refs_static, which was stabilized
in 1.83.0 and allows for much-improved vmstate bindings. These were
prototyped by Zhao and myself and I'll post them shortly for reference.
It's unlikely that Debian bookworm will update rustc-web any further,
since the next Firefox ESR version is expected roughly at the same time
as the Debian trixie release.
Paolo
Paolo Bonzini (9):
lcitool: use Rust 1.78 for Debian bookworm
rust: use std::ffi instead of std::os::raw
rust: let bilge use "let ... else"
rust: qemu_api_macros: use "let ... else"
rust: use MaybeUninit::zeroed() in const context
rust: remove offset_of replacement
rust: replace c_str! with c"" literals
rust: enable clippy::ptr_cast_constness
docs: rust: update for newer minimum supported version
docs/about/build-platforms.rst | 7 +-
docs/devel/rust.rst | 38 +---
rust/Cargo.lock | 1 -
rust/Cargo.toml | 4 +-
rust/clippy.toml | 2 +-
rust/hw/char/pl011/Cargo.toml | 2 +-
rust/hw/char/pl011/src/device.rs | 4 +-
rust/hw/char/pl011/src/device_class.rs | 13 +-
rust/hw/char/pl011/src/lib.rs | 6 +-
rust/hw/timer/hpet/Cargo.toml | 2 +-
rust/hw/timer/hpet/src/fw_cfg.rs | 6 +-
rust/hw/timer/hpet/src/hpet.rs | 28 ++-
rust/hw/timer/hpet/src/lib.rs | 4 +-
rust/qemu-api-macros/Cargo.toml | 2 +-
rust/qemu-api-macros/src/lib.rs | 113 +++++-------
rust/qemu-api/Cargo.toml | 5 +-
rust/qemu-api/build.rs | 7 -
rust/qemu-api/meson.build | 5 -
rust/qemu-api/src/c_str.rs | 61 -------
rust/qemu-api/src/cell.rs | 6 +-
rust/qemu-api/src/chardev.rs | 5 +-
rust/qemu-api/src/irq.rs | 6 +-
rust/qemu-api/src/lib.rs | 7 +-
rust/qemu-api/src/memory.rs | 3 +-
rust/qemu-api/src/offset_of.rs | 168 ------------------
rust/qemu-api/src/qdev.rs | 9 +-
rust/qemu-api/src/qom.rs | 7 +-
rust/qemu-api/src/timer.rs | 4 +-
rust/qemu-api/src/vmstate.rs | 14 +-
rust/qemu-api/src/zeroable.rs | 104 ++---------
rust/qemu-api/tests/tests.rs | 11 +-
rust/qemu-api/tests/vmstate_tests.rs | 27 +--
subprojects/bilge-impl-0.2-rs.wrap | 2 -
.../packagefiles/bilge-impl-1.63.0.patch | 45 -----
tests/lcitool/mappings.yml | 3 +
35 files changed, 146 insertions(+), 585 deletions(-)
delete mode 100644 rust/qemu-api/src/c_str.rs
delete mode 100644 rust/qemu-api/src/offset_of.rs
delete mode 100644 subprojects/packagefiles/bilge-impl-1.63.0.patch
--
2.49.0
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 1/9] lcitool: use Rust 1.78 for Debian bookworm
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 10:23 ` [PATCH 2/9] rust: use std::ffi instead of std::os::raw Paolo Bonzini
` (8 subsequent siblings)
9 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
The rustc-web package provides a newer Rust compiler (1.78) on all
architectures except mips64el.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/about/build-platforms.rst | 7 ++++---
tests/lcitool/mappings.yml | 3 +++
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/docs/about/build-platforms.rst b/docs/about/build-platforms.rst
index 52521552c8a..bfcbb431f10 100644
--- a/docs/about/build-platforms.rst
+++ b/docs/about/build-platforms.rst
@@ -118,9 +118,10 @@ Rust build dependencies
include bindgen or have an older version, it is recommended to install
a newer version using ``cargo install bindgen-cli``.
- Developers may want to use Cargo-based tools in the QEMU source tree;
- this requires Cargo 1.74.0. Note that Cargo is not required in order
- to build QEMU.
+ QEMU requires Rust 1.77.0. This is available on all supported platforms
+ with one exception, namely the ``mips64el`` architecture on Debian bookworm.
+ For all other architectures, Debian bookworm provides a new-enough Rust
+ compiler in the ``rustc-web`` package.
Optional build dependencies
Build components whose absence does not affect the ability to build QEMU
diff --git a/tests/lcitool/mappings.yml b/tests/lcitool/mappings.yml
index 74eb13d62b6..2ac304b72ff 100644
--- a/tests/lcitool/mappings.yml
+++ b/tests/lcitool/mappings.yml
@@ -64,6 +64,9 @@ mappings:
python3-wheel:
OpenSUSELeap15: python311-pip
+ rust:
+ Debian12: rustc-web
+
pypi_mappings:
# Request more recent version
meson:
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 2/9] rust: use std::ffi instead of std::os::raw
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
2025-05-02 10:23 ` [PATCH 1/9] lcitool: use Rust 1.78 for Debian bookworm Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 10:57 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 3/9] rust: let bilge use "let ... else" Paolo Bonzini
` (7 subsequent siblings)
9 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
This is allowed since Rust 1.64.0.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 2 --
rust/hw/char/pl011/src/device_class.rs | 2 +-
rust/hw/timer/hpet/src/hpet.rs | 3 +--
rust/qemu-api/src/chardev.rs | 3 +--
rust/qemu-api/src/irq.rs | 6 +++++-
rust/qemu-api/src/lib.rs | 2 +-
rust/qemu-api/src/memory.rs | 3 +--
rust/qemu-api/src/qdev.rs | 3 +--
rust/qemu-api/src/qom.rs | 3 +--
rust/qemu-api/src/timer.rs | 2 +-
rust/qemu-api/src/vmstate.rs | 2 +-
rust/qemu-api/tests/vmstate_tests.rs | 7 ++++++-
12 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 3cc2841d4d1..c7dfba98de1 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -74,8 +74,6 @@ Supported tools
QEMU supports rustc version 1.63.0 and newer. Notably, the following features
are missing:
-* ``core::ffi`` (1.64.0). Use ``std::os::raw`` and ``std::ffi`` instead.
-
* ``cast_mut()``/``cast_const()`` (1.65.0). Use ``as`` instead.
* "let ... else" (1.65.0). Use ``if let`` instead. This is currently patched
diff --git a/rust/hw/char/pl011/src/device_class.rs b/rust/hw/char/pl011/src/device_class.rs
index b4d4a7eb432..cd2dc33c207 100644
--- a/rust/hw/char/pl011/src/device_class.rs
+++ b/rust/hw/char/pl011/src/device_class.rs
@@ -3,7 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
use std::{
- os::raw::{c_int, c_void},
+ ffi::{c_int, c_void},
ptr::NonNull,
};
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index 12de2ba59a1..d4364f2f2f7 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -3,8 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
use std::{
- ffi::CStr,
- os::raw::{c_int, c_void},
+ ffi::{c_int, c_void, CStr},
pin::Pin,
ptr::{addr_of_mut, null_mut, NonNull},
slice::from_ref,
diff --git a/rust/qemu-api/src/chardev.rs b/rust/qemu-api/src/chardev.rs
index 11e6c45afaf..146a4852da3 100644
--- a/rust/qemu-api/src/chardev.rs
+++ b/rust/qemu-api/src/chardev.rs
@@ -10,11 +10,10 @@
//! called.
use std::{
- ffi::CStr,
+ ffi::{c_int, c_void, CStr},
fmt::{self, Debug},
io::{self, ErrorKind, Write},
marker::PhantomPinned,
- os::raw::{c_int, c_void},
ptr::addr_of_mut,
slice,
};
diff --git a/rust/qemu-api/src/irq.rs b/rust/qemu-api/src/irq.rs
index 1222d4fde30..1526e6f63a1 100644
--- a/rust/qemu-api/src/irq.rs
+++ b/rust/qemu-api/src/irq.rs
@@ -4,7 +4,11 @@
//! Bindings for interrupt sources
-use std::{ffi::CStr, marker::PhantomData, os::raw::c_int, ptr};
+use std::{
+ ffi::{c_int, CStr},
+ marker::PhantomData,
+ ptr,
+};
use crate::{
bindings::{self, qemu_set_irq},
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index 05f38b51d30..c3641e893b7 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -33,7 +33,7 @@
use std::{
alloc::{GlobalAlloc, Layout},
- os::raw::c_void,
+ ffi::c_void,
};
#[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
diff --git a/rust/qemu-api/src/memory.rs b/rust/qemu-api/src/memory.rs
index fdb1ea11fcf..9ef2694bd62 100644
--- a/rust/qemu-api/src/memory.rs
+++ b/rust/qemu-api/src/memory.rs
@@ -5,9 +5,8 @@
//! Bindings for `MemoryRegion`, `MemoryRegionOps` and `MemTxAttrs`
use std::{
- ffi::{CStr, CString},
+ ffi::{c_uint, c_void, CStr, CString},
marker::PhantomData,
- os::raw::{c_uint, c_void},
};
pub use bindings::{hwaddr, MemTxAttrs};
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 18b4a9ba687..17c7fecabeb 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -5,8 +5,7 @@
//! Bindings to create devices and access device functionality from Rust.
use std::{
- ffi::{CStr, CString},
- os::raw::{c_int, c_void},
+ ffi::{c_int, c_void, CStr, CString},
ptr::NonNull,
};
diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs
index f1b4022157b..6929e4d33ae 100644
--- a/rust/qemu-api/src/qom.rs
+++ b/rust/qemu-api/src/qom.rs
@@ -93,11 +93,10 @@
//! without incurring into violations of orphan rules for traits.
use std::{
- ffi::CStr,
+ ffi::{c_void, CStr},
fmt,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
- os::raw::c_void,
ptr::NonNull,
};
diff --git a/rust/qemu-api/src/timer.rs b/rust/qemu-api/src/timer.rs
index e769f8bc910..d697fd742bc 100644
--- a/rust/qemu-api/src/timer.rs
+++ b/rust/qemu-api/src/timer.rs
@@ -3,7 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
use std::{
- os::raw::{c_int, c_void},
+ ffi::{c_int, c_void},
pin::Pin,
};
diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
index 98152e5964d..78c7f17db13 100644
--- a/rust/qemu-api/src/vmstate.rs
+++ b/rust/qemu-api/src/vmstate.rs
@@ -25,7 +25,7 @@
//! functionality that is missing from `vmstate_of!`.
use core::{marker::PhantomData, mem, ptr::NonNull};
-use std::os::raw::{c_int, c_void};
+use std::ffi::{c_int, c_void};
pub use crate::bindings::{VMStateDescription, VMStateField};
use crate::{
diff --git a/rust/qemu-api/tests/vmstate_tests.rs b/rust/qemu-api/tests/vmstate_tests.rs
index f7a93117e1a..9a56ffd2385 100644
--- a/rust/qemu-api/tests/vmstate_tests.rs
+++ b/rust/qemu-api/tests/vmstate_tests.rs
@@ -2,7 +2,12 @@
// Author(s): Zhao Liu <zhai1.liu@intel.com>
// SPDX-License-Identifier: GPL-2.0-or-later
-use std::{ffi::CStr, mem::size_of, os::raw::c_void, ptr::NonNull, slice};
+use std::{
+ ffi::{c_void, CStr},
+ mem::size_of,
+ ptr::NonNull,
+ slice,
+};
use qemu_api::{
bindings::{
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 3/9] rust: let bilge use "let ... else"
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
2025-05-02 10:23 ` [PATCH 1/9] lcitool: use Rust 1.78 for Debian bookworm Paolo Bonzini
2025-05-02 10:23 ` [PATCH 2/9] rust: use std::ffi instead of std::os::raw Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 12:44 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 4/9] rust: qemu_api_macros: " Paolo Bonzini
` (6 subsequent siblings)
9 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 3 --
subprojects/bilge-impl-0.2-rs.wrap | 2 -
.../packagefiles/bilge-impl-1.63.0.patch | 45 -------------------
3 files changed, 50 deletions(-)
delete mode 100644 subprojects/packagefiles/bilge-impl-1.63.0.patch
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index c7dfba98de1..8167ff49aa9 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -76,9 +76,6 @@ are missing:
* ``cast_mut()``/``cast_const()`` (1.65.0). Use ``as`` instead.
-* "let ... else" (1.65.0). Use ``if let`` instead. This is currently patched
- in QEMU's vendored copy of the bilge crate.
-
* Generic Associated Types (1.65.0)
* ``CStr::from_bytes_with_nul()`` as a ``const`` function (1.72.0).
diff --git a/subprojects/bilge-impl-0.2-rs.wrap b/subprojects/bilge-impl-0.2-rs.wrap
index d14c3dc769b..4ccd376eb98 100644
--- a/subprojects/bilge-impl-0.2-rs.wrap
+++ b/subprojects/bilge-impl-0.2-rs.wrap
@@ -4,8 +4,6 @@ source_url = https://crates.io/api/v1/crates/bilge-impl/0.2.0/download
source_filename = bilge-impl-0.2.0.tar.gz
source_hash = feb11e002038ad243af39c2068c8a72bcf147acf05025dcdb916fcc000adb2d8
#method = cargo
-patch_directory = bilge-impl-0.2-rs
-diff_files = bilge-impl-1.63.0.patch
# bump this version number on every change to meson.build or the patches:
# v2
diff --git a/subprojects/packagefiles/bilge-impl-1.63.0.patch b/subprojects/packagefiles/bilge-impl-1.63.0.patch
deleted file mode 100644
index 987428a6d65..00000000000
--- a/subprojects/packagefiles/bilge-impl-1.63.0.patch
+++ /dev/null
@@ -1,45 +0,0 @@
---- a/src/shared/discriminant_assigner.rs
-+++ b/src/shared/discriminant_assigner.rs
-@@ -26,20 +26,20 @@
- let discriminant_expr = &discriminant.1;
- let variant_name = &variant.ident;
-
-- let Expr::Lit(ExprLit { lit: Lit::Int(int), .. }) = discriminant_expr else {
-+ if let Expr::Lit(ExprLit { lit: Lit::Int(int), .. }) = discriminant_expr {
-+ let discriminant_value: u128 = int.base10_parse().unwrap_or_else(unreachable);
-+ if discriminant_value > self.max_value() {
-+ abort!(variant, "Value of variant exceeds the given number of bits")
-+ }
-+
-+ Some(discriminant_value)
-+ } else {
- abort!(
- discriminant_expr,
- "variant `{}` is not a number", variant_name;
- help = "only literal integers currently supported"
- )
-- };
--
-- let discriminant_value: u128 = int.base10_parse().unwrap_or_else(unreachable);
-- if discriminant_value > self.max_value() {
-- abort!(variant, "Value of variant exceeds the given number of bits")
- }
--
-- Some(discriminant_value)
- }
-
- fn assign(&mut self, variant: &Variant) -> u128 {
---- a/src/shared/fallback.rs
-+++ b/src/shared/fallback.rs
-@@ -22,8 +22,9 @@
- }
- Unnamed(fields) => {
- let variant_fields = fields.unnamed.iter();
-- let Ok(fallback_value) = variant_fields.exactly_one() else {
-- abort!(variant, "fallback variant must have exactly one field"; help = "use only one field or change to a unit variant")
-+ let fallback_value = match variant_fields.exactly_one() {
-+ Ok(ok) => ok,
-+ _ => abort!(variant, "fallback variant must have exactly one field"; help = "use only one field or change to a unit variant")
- };
-
- if !is_last_variant {
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 4/9] rust: qemu_api_macros: use "let ... else"
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (2 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 3/9] rust: let bilge use "let ... else" Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 12:45 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
` (5 subsequent siblings)
9 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
"let ... else" is useful when visiting syntax trees, to avoid multiple
levels of indentation.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api-macros/src/lib.rs | 84 ++++++++++++++++-----------------
1 file changed, 40 insertions(+), 44 deletions(-)
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index eda0d46d122..1448f3025a4 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -16,50 +16,47 @@ fn get_fields<'a>(
input: &'a DeriveInput,
msg: &str,
) -> Result<&'a Punctuated<Field, Comma>, MacroError> {
- if let Data::Struct(s) = &input.data {
- if let Fields::Named(fs) = &s.fields {
- Ok(&fs.named)
- } else {
- Err(MacroError::Message(
- format!("Named fields required for {}", msg),
- input.ident.span(),
- ))
- }
- } else {
- Err(MacroError::Message(
+ let Data::Struct(s) = &input.data else {
+ return Err(MacroError::Message(
format!("Struct required for {}", msg),
input.ident.span(),
))
- }
+ };
+ let Fields::Named(fs) = &s.fields else {
+ return Err(MacroError::Message(
+ format!("Named fields required for {}", msg),
+ input.ident.span(),
+ ))
+ };
+ Ok(&fs.named)
}
fn get_unnamed_field<'a>(input: &'a DeriveInput, msg: &str) -> Result<&'a Field, MacroError> {
- if let Data::Struct(s) = &input.data {
- let unnamed = match &s.fields {
- Fields::Unnamed(FieldsUnnamed {
- unnamed: ref fields,
- ..
- }) => fields,
- _ => {
- return Err(MacroError::Message(
- format!("Tuple struct required for {}", msg),
- s.fields.span(),
- ))
- }
- };
- if unnamed.len() != 1 {
- return Err(MacroError::Message(
- format!("A single field is required for {}", msg),
- s.fields.span(),
- ));
- }
- Ok(&unnamed[0])
- } else {
- Err(MacroError::Message(
+ let Data::Struct(s) = &input.data else {
+ return Err(MacroError::Message(
format!("Struct required for {}", msg),
input.ident.span(),
))
+ };
+ let unnamed = match &s.fields {
+ Fields::Unnamed(FieldsUnnamed {
+ unnamed: ref fields,
+ ..
+ }) => fields,
+ _ => {
+ return Err(MacroError::Message(
+ format!("Tuple struct required for {}", msg),
+ s.fields.span(),
+ ))
+ }
+ };
+ if unnamed.len() != 1 {
+ return Err(MacroError::Message(
+ format!("A single field is required for {}", msg),
+ s.fields.span(),
+ ));
}
+ Ok(&unnamed[0])
}
fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), MacroError> {
@@ -210,20 +207,19 @@ fn get_repr_uN(input: &DeriveInput, msg: &str) -> Result<Path, MacroError> {
}
fn get_variants(input: &DeriveInput) -> Result<&Punctuated<Variant, Comma>, MacroError> {
- if let Data::Enum(e) = &input.data {
- if let Some(v) = e.variants.iter().find(|v| v.fields != Fields::Unit) {
- return Err(MacroError::Message(
- "Cannot derive TryInto for enum with non-unit variants.".to_string(),
- v.fields.span(),
- ));
- }
- Ok(&e.variants)
- } else {
- Err(MacroError::Message(
+ let Data::Enum(e) = &input.data else {
+ return Err(MacroError::Message(
"Cannot derive TryInto for union or struct.".to_string(),
input.ident.span(),
))
+ };
+ if let Some(v) = e.variants.iter().find(|v| v.fields != Fields::Unit) {
+ return Err(MacroError::Message(
+ "Cannot derive TryInto for enum with non-unit variants.".to_string(),
+ v.fields.span(),
+ ));
}
+ Ok(&e.variants)
}
#[rustfmt::skip::macros(quote)]
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (3 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 4/9] rust: qemu_api_macros: " Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 11:01 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 6/9] rust: remove offset_of replacement Paolo Bonzini
` (4 subsequent siblings)
9 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 4 --
rust/hw/timer/hpet/src/fw_cfg.rs | 6 +-
rust/qemu-api/src/zeroable.rs | 104 +++++--------------------------
3 files changed, 18 insertions(+), 96 deletions(-)
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 8167ff49aa9..13a002cfe69 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -83,10 +83,6 @@ are missing:
* "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
the pinned-init create).
-* ``MaybeUninit::zeroed()`` as a ``const`` function (1.75.0). QEMU's
- ``Zeroable`` trait can be implemented without ``MaybeUninit::zeroed()``,
- so this would be just a cleanup.
-
* ``c"" literals`` (stable in 1.77.0). QEMU provides a ``c_str!()`` macro
to define ``CStr`` constants easily
diff --git a/rust/hw/timer/hpet/src/fw_cfg.rs b/rust/hw/timer/hpet/src/fw_cfg.rs
index bef03727ea3..aa08d283519 100644
--- a/rust/hw/timer/hpet/src/fw_cfg.rs
+++ b/rust/hw/timer/hpet/src/fw_cfg.rs
@@ -4,7 +4,7 @@
use std::ptr::addr_of_mut;
-use qemu_api::{cell::bql_locked, impl_zeroable, zeroable::Zeroable};
+use qemu_api::{cell::bql_locked, zeroable::Zeroable};
/// Each `HPETState` represents a Event Timer Block. The v1 spec supports
/// up to 8 blocks. QEMU only uses 1 block (in PC machine).
@@ -18,7 +18,7 @@ pub struct HPETFwEntry {
pub min_tick: u16,
pub page_prot: u8,
}
-impl_zeroable!(HPETFwEntry);
+unsafe impl Zeroable for HPETFwEntry {}
#[repr(C, packed)]
#[derive(Copy, Clone, Default)]
@@ -26,7 +26,7 @@ pub struct HPETFwConfig {
pub count: u8,
pub hpet: [HPETFwEntry; HPET_MAX_NUM_EVENT_TIMER_BLOCK],
}
-impl_zeroable!(HPETFwConfig);
+unsafe impl Zeroable for HPETFwConfig {}
#[allow(non_upper_case_globals)]
#[no_mangle]
diff --git a/rust/qemu-api/src/zeroable.rs b/rust/qemu-api/src/zeroable.rs
index a3415a2ebcc..57d802db69b 100644
--- a/rust/qemu-api/src/zeroable.rs
+++ b/rust/qemu-api/src/zeroable.rs
@@ -4,89 +4,15 @@
/// Encapsulates the requirement that
/// `MaybeUninit::<Self>::zeroed().assume_init()` does not cause undefined
-/// behavior. This trait in principle could be implemented as just:
-///
-/// ```
-/// pub unsafe trait Zeroable: Default {
-/// const ZERO: Self = unsafe { ::core::mem::MaybeUninit::<Self>::zeroed().assume_init() };
-/// }
-/// ```
-///
-/// The need for a manual implementation is only because `zeroed()` cannot
-/// be used as a `const fn` prior to Rust 1.75.0. Once we can assume a new
-/// enough version of the compiler, we could provide a `#[derive(Zeroable)]`
-/// macro to check at compile-time that all struct fields are Zeroable, and
-/// use the above blanket implementation of the `ZERO` constant.
+/// behavior.
///
/// # Safety
///
-/// Because the implementation of `ZERO` is manual, it does not make
-/// any assumption on the safety of `zeroed()`. However, other users of the
-/// trait could use it that way. Do not add this trait to a type unless
-/// all-zeroes is a valid value for the type. In particular, remember that
-/// raw pointers can be zero, but references and `NonNull<T>` cannot
+/// Do not add this trait to a type unless all-zeroes is a valid value for the
+/// type. In particular, raw pointers can be zero, but references and `NonNull<T>`
+/// cannot.
pub unsafe trait Zeroable: Default {
- const ZERO: Self;
-}
-
-/// A macro that acts similarly to [`core::mem::zeroed()`], only is const
-///
-/// ## Safety
-///
-/// Similar to `core::mem::zeroed()`, except this zeroes padding bits. Zeroed
-/// padding usually isn't relevant to safety, but might be if a C union is used.
-///
-/// Just like for `core::mem::zeroed()`, an all zero byte pattern might not
-/// be a valid value for a type, as is the case for references `&T` and `&mut
-/// T`. Reference types trigger a (denied by default) lint and cause immediate
-/// undefined behavior if the lint is ignored
-///
-/// ```rust compile_fail
-/// use const_zero::const_zero;
-/// // error: any use of this value will cause an error
-/// // note: `#[deny(const_err)]` on by default
-/// const STR: &str = unsafe{const_zero!(&'static str)};
-/// ```
-///
-/// `const_zero` does not work on unsized types:
-///
-/// ```rust compile_fail
-/// use const_zero::const_zero;
-/// // error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
-/// const BYTES: [u8] = unsafe{const_zero!([u8])};
-/// ```
-/// ## Differences with `core::mem::zeroed`
-///
-/// `const_zero` zeroes padding bits, while `core::mem::zeroed` doesn't
-#[macro_export]
-macro_rules! const_zero {
- // This macro to produce a type-generic zero constant is taken from the
- // const_zero crate (v0.1.1):
- //
- // https://docs.rs/const-zero/latest/src/const_zero/lib.rs.html
- //
- // and used under MIT license
- ($type_:ty) => {{
- const TYPE_SIZE: ::core::primitive::usize = ::core::mem::size_of::<$type_>();
- union TypeAsBytes {
- bytes: [::core::primitive::u8; TYPE_SIZE],
- inner: ::core::mem::ManuallyDrop<$type_>,
- }
- const ZERO_BYTES: TypeAsBytes = TypeAsBytes {
- bytes: [0; TYPE_SIZE],
- };
- ::core::mem::ManuallyDrop::<$type_>::into_inner(ZERO_BYTES.inner)
- }};
-}
-
-/// A wrapper to implement the `Zeroable` trait through the `const_zero` macro.
-#[macro_export]
-macro_rules! impl_zeroable {
- ($type:ty) => {
- unsafe impl $crate::zeroable::Zeroable for $type {
- const ZERO: Self = unsafe { $crate::const_zero!($type) };
- }
- };
+ const ZERO: Self = unsafe { ::core::mem::MaybeUninit::<Self>::zeroed().assume_init() };
}
// bindgen does not derive Default here
@@ -97,13 +23,13 @@ fn default() -> Self {
}
}
-impl_zeroable!(crate::bindings::Property__bindgen_ty_1);
-impl_zeroable!(crate::bindings::Property);
-impl_zeroable!(crate::bindings::VMStateFlags);
-impl_zeroable!(crate::bindings::VMStateField);
-impl_zeroable!(crate::bindings::VMStateDescription);
-impl_zeroable!(crate::bindings::MemoryRegionOps__bindgen_ty_1);
-impl_zeroable!(crate::bindings::MemoryRegionOps__bindgen_ty_2);
-impl_zeroable!(crate::bindings::MemoryRegionOps);
-impl_zeroable!(crate::bindings::MemTxAttrs);
-impl_zeroable!(crate::bindings::CharBackend);
+unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {}
+unsafe impl Zeroable for crate::bindings::Property {}
+unsafe impl Zeroable for crate::bindings::VMStateFlags {}
+unsafe impl Zeroable for crate::bindings::VMStateField {}
+unsafe impl Zeroable for crate::bindings::VMStateDescription {}
+unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 {}
+unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 {}
+unsafe impl Zeroable for crate::bindings::MemoryRegionOps {}
+unsafe impl Zeroable for crate::bindings::MemTxAttrs {}
+unsafe impl Zeroable for crate::bindings::CharBackend {}
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 6/9] rust: remove offset_of replacement
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (4 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 10:23 ` [PATCH 7/9] rust: replace c_str! with c"" literals Paolo Bonzini
` (3 subsequent siblings)
9 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
---
docs/devel/rust.rst | 6 -
rust/Cargo.toml | 2 +-
rust/hw/char/pl011/src/device.rs | 4 +-
rust/hw/timer/hpet/src/hpet.rs | 4 +-
rust/qemu-api-macros/src/lib.rs | 29 +----
rust/qemu-api/Cargo.toml | 3 -
rust/qemu-api/build.rs | 7 --
rust/qemu-api/meson.build | 4 -
rust/qemu-api/src/lib.rs | 4 -
rust/qemu-api/src/offset_of.rs | 168 ---------------------------
rust/qemu-api/src/qdev.rs | 6 +-
rust/qemu-api/src/vmstate.rs | 10 +-
rust/qemu-api/tests/tests.rs | 2 -
rust/qemu-api/tests/vmstate_tests.rs | 5 +-
14 files changed, 16 insertions(+), 238 deletions(-)
delete mode 100644 rust/qemu-api/src/offset_of.rs
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 13a002cfe69..8dad90fdf98 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -86,12 +86,6 @@ are missing:
* ``c"" literals`` (stable in 1.77.0). QEMU provides a ``c_str!()`` macro
to define ``CStr`` constants easily
-* ``offset_of!`` (stable in 1.77.0). QEMU uses ``offset_of!()`` heavily; it
- provides a replacement in the ``qemu_api`` crate, but it does not support
- lifetime parameters and therefore ``&'a Something`` fields in the struct
- may have to be replaced by ``NonNull<Something>``. *Nested* ``offset_of!``
- was only stabilized in Rust 1.82.0, but it is not used.
-
* inline const expression (stable in 1.79.0), currently worked around with
associated constants in the ``FnCall`` trait.
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index ab1185a8143..ea2a85a1d07 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -10,7 +10,7 @@ members = [
[workspace.lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = [
'cfg(MESON)', 'cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)',
- 'cfg(has_offset_of)'] }
+] }
# Occasionally, we may need to silence warnings and clippy lints that
# were only introduced in newer Rust compiler versions. Do not croak
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index bb2a0f207a5..7c563ade9cd 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -74,7 +74,7 @@ fn index(&self, idx: u32) -> &Self::Output {
}
#[repr(C)]
-#[derive(Debug, Default, qemu_api_macros::offsets)]
+#[derive(Debug, Default)]
pub struct PL011Registers {
#[doc(alias = "fr")]
pub flags: registers::Flags,
@@ -98,7 +98,7 @@ pub struct PL011Registers {
}
#[repr(C)]
-#[derive(qemu_api_macros::Object, qemu_api_macros::offsets)]
+#[derive(qemu_api_macros::Object)]
/// PL011 Device Model in QEMU
pub struct PL011State {
pub parent_obj: ParentField<SysBusDevice>,
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index d4364f2f2f7..72609c31f44 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -183,7 +183,7 @@ fn timer_handler(timer_cell: &BqlRefCell<HPETTimer>) {
/// HPET Timer Abstraction
#[repr(C)]
-#[derive(Debug, qemu_api_macros::offsets)]
+#[derive(Debug)]
pub struct HPETTimer {
/// timer N index within the timer block (`HPETState`)
#[doc(alias = "tn")]
@@ -523,7 +523,7 @@ fn write(&mut self, reg: TimerRegister, value: u64, shift: u32, len: u32) {
/// HPET Event Timer Block Abstraction
#[repr(C)]
-#[derive(qemu_api_macros::Object, qemu_api_macros::offsets)]
+#[derive(qemu_api_macros::Object)]
pub struct HPETState {
parent_obj: ParentField<SysBusDevice>,
iomem: MemoryRegion,
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index 1448f3025a4..108f434ed44 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -6,7 +6,7 @@
use quote::quote;
use syn::{
parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, token::Comma, Data,
- DeriveInput, Field, Fields, FieldsUnnamed, Ident, Meta, Path, Token, Type, Variant, Visibility,
+ DeriveInput, Field, Fields, FieldsUnnamed, Ident, Meta, Path, Token, Variant,
};
mod utils;
@@ -157,33 +157,6 @@ pub fn derive_opaque(input: TokenStream) -> TokenStream {
TokenStream::from(expanded)
}
-#[rustfmt::skip::macros(quote)]
-fn derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> {
- is_c_repr(&input, "#[derive(offsets)]")?;
-
- let name = &input.ident;
- let fields = get_fields(&input, "#[derive(offsets)]")?;
- let field_names: Vec<&Ident> = fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();
- let field_types: Vec<&Type> = fields.iter().map(|f| &f.ty).collect();
- let field_vis: Vec<&Visibility> = fields.iter().map(|f| &f.vis).collect();
-
- Ok(quote! {
- ::qemu_api::with_offsets! {
- struct #name {
- #(#field_vis #field_names: #field_types,)*
- }
- }
- })
-}
-
-#[proc_macro_derive(offsets)]
-pub fn derive_offsets(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as DeriveInput);
- let expanded = derive_offsets_or_error(input).unwrap_or_else(Into::into);
-
- TokenStream::from(expanded)
-}
-
#[allow(non_snake_case)]
fn get_repr_uN(input: &DeriveInput, msg: &str) -> Result<Path, MacroError> {
let repr = input.attrs.iter().find(|attr| attr.path().is_ident("repr"));
diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
index 57747bc9341..c5b7b2464e9 100644
--- a/rust/qemu-api/Cargo.toml
+++ b/rust/qemu-api/Cargo.toml
@@ -18,9 +18,6 @@ rust-version = "1.63.0"
qemu_api_macros = { path = "../qemu-api-macros" }
libc = "0.2.162"
-[build-dependencies]
-version_check = "~0.9"
-
[features]
default = ["debug_cell"]
allocator = []
diff --git a/rust/qemu-api/build.rs b/rust/qemu-api/build.rs
index 471e6c633df..470be2a4d54 100644
--- a/rust/qemu-api/build.rs
+++ b/rust/qemu-api/build.rs
@@ -8,8 +8,6 @@
use std::os::windows::fs::symlink_file;
use std::{env, fs::remove_file, io::Result, path::Path};
-use version_check as rustc;
-
fn main() -> Result<()> {
// Placing bindings.inc.rs in the source directory is supported
// but not documented or encouraged.
@@ -38,11 +36,6 @@ fn main() -> Result<()> {
}
symlink_file(file, dest_path)?;
- // Check for available rustc features
- if rustc::is_min_version("1.77.0").unwrap_or(false) {
- println!("cargo:rustc-cfg=has_offset_of");
- }
-
println!("cargo:rerun-if-changed=build.rs");
Ok(())
}
diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
index 858685ddd4a..95e65643ae1 100644
--- a/rust/qemu-api/meson.build
+++ b/rust/qemu-api/meson.build
@@ -5,9 +5,6 @@ _qemu_api_cfg = run_command(rustc_args,
libc_dep = dependency('libc-0.2-rs')
# _qemu_api_cfg += ['--cfg', 'feature="allocator"']
-if rustc.version().version_compare('>=1.77.0')
- _qemu_api_cfg += ['--cfg', 'has_offset_of']
-endif
if get_option('debug_mutex')
_qemu_api_cfg += ['--cfg', 'feature="debug_cell"']
endif
@@ -28,7 +25,6 @@ _qemu_api_rs = static_library(
'src/irq.rs',
'src/memory.rs',
'src/module.rs',
- 'src/offset_of.rs',
'src/prelude.rs',
'src/qdev.rs',
'src/qom.rs',
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index c3641e893b7..90c19636660 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -23,7 +23,6 @@
pub mod irq;
pub mod memory;
pub mod module;
-pub mod offset_of;
pub mod qdev;
pub mod qom;
pub mod sysbus;
@@ -165,6 +164,3 @@ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
}
}
}
-
-#[cfg(has_offset_of)]
-pub use core::mem::offset_of;
diff --git a/rust/qemu-api/src/offset_of.rs b/rust/qemu-api/src/offset_of.rs
deleted file mode 100644
index 373229bbde9..00000000000
--- a/rust/qemu-api/src/offset_of.rs
+++ /dev/null
@@ -1,168 +0,0 @@
-// SPDX-License-Identifier: MIT
-
-#![doc(hidden)]
-//! This module provides macros that emulate the functionality of
-//! `core::mem::offset_of` on older versions of Rust.
-//!
-//! Documentation is hidden because it only exposes macros, which
-//! are exported directly from `qemu_api`.
-
-/// This macro provides the same functionality as `core::mem::offset_of`,
-/// except that only one level of field access is supported. The declaration
-/// of the struct must be wrapped with `with_offsets! { }`.
-///
-/// It is needed because `offset_of!` was only stabilized in Rust 1.77.
-#[cfg(not(has_offset_of))]
-#[macro_export]
-macro_rules! offset_of {
- ($Container:ty, $field:ident) => {
- <$Container>::OFFSET_TO__.$field
- };
-}
-
-/// A wrapper for struct declarations, that allows using `offset_of!` in
-/// versions of Rust prior to 1.77
-#[macro_export]
-macro_rules! with_offsets {
- // This method to generate field offset constants comes from:
- //
- // https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10a22a9b8393abd7b541d8fc844bc0df
- //
- // used under MIT license with permission of Yandros aka Daniel Henry-Mantilla
- (
- $(#[$struct_meta:meta])*
- $struct_vis:vis
- struct $StructName:ident {
- $(
- $(#[$field_meta:meta])*
- $field_vis:vis
- $field_name:ident : $field_ty:ty
- ),*
- $(,)?
- }
- ) => (
- #[cfg(not(has_offset_of))]
- const _: () = {
- struct StructOffsetsHelper<T>(std::marker::PhantomData<T>);
- const END_OF_PREV_FIELD: usize = 0;
-
- // populate StructOffsetsHelper<T> with associated consts,
- // one for each field
- $crate::with_offsets! {
- @struct $StructName
- @names [ $($field_name)* ]
- @tys [ $($field_ty ,)*]
- }
-
- // now turn StructOffsetsHelper<T>'s consts into a single struct,
- // applying field visibility. This provides better error messages
- // than if offset_of! used StructOffsetsHelper::<T> directly.
- pub
- struct StructOffsets {
- $(
- $field_vis
- $field_name: usize,
- )*
- }
- impl $StructName {
- pub
- const OFFSET_TO__: StructOffsets = StructOffsets {
- $(
- $field_name: StructOffsetsHelper::<$StructName>::$field_name,
- )*
- };
- }
- };
- );
-
- (
- @struct $StructName:ident
- @names []
- @tys []
- ) => ();
-
- (
- @struct $StructName:ident
- @names [$field_name:ident $($other_names:tt)*]
- @tys [$field_ty:ty , $($other_tys:tt)*]
- ) => (
- #[allow(non_local_definitions)]
- #[allow(clippy::modulo_one)]
- impl StructOffsetsHelper<$StructName> {
- #[allow(nonstandard_style)]
- const $field_name: usize = {
- const ALIGN: usize = std::mem::align_of::<$field_ty>();
- const TRAIL: usize = END_OF_PREV_FIELD % ALIGN;
- END_OF_PREV_FIELD + (if TRAIL == 0 { 0usize } else { ALIGN - TRAIL })
- };
- }
- const _: () = {
- const END_OF_PREV_FIELD: usize =
- StructOffsetsHelper::<$StructName>::$field_name +
- std::mem::size_of::<$field_ty>()
- ;
- $crate::with_offsets! {
- @struct $StructName
- @names [$($other_names)*]
- @tys [$($other_tys)*]
- }
- };
- );
-}
-
-#[cfg(test)]
-mod tests {
- use crate::offset_of;
-
- #[repr(C)]
- struct Foo {
- a: u16,
- b: u32,
- c: u64,
- d: u16,
- }
-
- #[repr(C)]
- struct Bar {
- pub a: u16,
- pub b: u64,
- c: Foo,
- d: u64,
- }
-
- crate::with_offsets! {
- #[repr(C)]
- struct Bar {
- pub a: u16,
- pub b: u64,
- c: Foo,
- d: u64,
- }
- }
-
- #[repr(C)]
- pub struct Baz {
- b: u32,
- a: u8,
- }
- crate::with_offsets! {
- #[repr(C)]
- pub struct Baz {
- b: u32,
- a: u8,
- }
- }
-
- #[test]
- fn test_offset_of() {
- const OFFSET_TO_C: usize = offset_of!(Bar, c);
-
- assert_eq!(offset_of!(Bar, a), 0);
- assert_eq!(offset_of!(Bar, b), 8);
- assert_eq!(OFFSET_TO_C, 16);
- assert_eq!(offset_of!(Bar, d), 40);
-
- assert_eq!(offset_of!(Baz, b), 0);
- assert_eq!(offset_of!(Baz, a), 4);
- }
-}
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 17c7fecabeb..1279d7a58d5 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -190,7 +190,7 @@ macro_rules! define_property {
// use associated function syntax for type checking
name: ::std::ffi::CStr::as_ptr($name),
info: $prop,
- offset: $crate::offset_of!($state, $field) as isize,
+ offset: ::std::mem::offset_of!($state, $field) as isize,
bitnr: $bitnr,
set_default: true,
defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
@@ -202,7 +202,7 @@ macro_rules! define_property {
// use associated function syntax for type checking
name: ::std::ffi::CStr::as_ptr($name),
info: $prop,
- offset: $crate::offset_of!($state, $field) as isize,
+ offset: ::std::mem::offset_of!($state, $field) as isize,
set_default: true,
defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
..$crate::zeroable::Zeroable::ZERO
@@ -213,7 +213,7 @@ macro_rules! define_property {
// use associated function syntax for type checking
name: ::std::ffi::CStr::as_ptr($name),
info: $prop,
- offset: $crate::offset_of!($state, $field) as isize,
+ offset: ::std::mem::offset_of!($state, $field) as isize,
set_default: false,
..$crate::zeroable::Zeroable::ZERO
}
diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
index 78c7f17db13..9a6e6b734b9 100644
--- a/rust/qemu-api/src/vmstate.rs
+++ b/rust/qemu-api/src/vmstate.rs
@@ -205,8 +205,8 @@ macro_rules! vmstate_of {
name: ::core::concat!(::core::stringify!($field_name), "\0")
.as_bytes()
.as_ptr() as *const ::std::os::raw::c_char,
- offset: $crate::offset_of!($struct_name, $field_name),
- $(num_offset: $crate::offset_of!($struct_name, $num),)?
+ offset: ::std::mem::offset_of!($struct_name, $field_name),
+ $(num_offset: ::std::mem::offset_of!($struct_name, $num),)?
// The calls to `call_func_with_field!` are the magic that
// computes most of the VMStateField from the type of the field.
info: $crate::info_enum_to_ref!($crate::call_func_with_field!(
@@ -482,10 +482,10 @@ macro_rules! vmstate_struct {
name: ::core::concat!(::core::stringify!($field_name), "\0")
.as_bytes()
.as_ptr() as *const ::std::os::raw::c_char,
- $(num_offset: $crate::offset_of!($struct_name, $num),)?
+ $(num_offset: ::std::mem::offset_of!($struct_name, $num),)?
offset: {
$crate::assert_field_type!($struct_name, $field_name, $type $(, num = $num)?);
- $crate::offset_of!($struct_name, $field_name)
+ ::std::mem::offset_of!($struct_name, $field_name)
},
size: ::core::mem::size_of::<$type>(),
flags: $crate::bindings::VMStateFlags::VMS_STRUCT,
@@ -517,7 +517,7 @@ macro_rules! vmstate_clock {
$field_name,
$crate::qom::Owned<$crate::qdev::Clock> $(, num = $num)?
);
- $crate::offset_of!($struct_name, $field_name)
+ ::std::mem::offset_of!($struct_name, $field_name)
},
size: ::core::mem::size_of::<*const $crate::qdev::Clock>(),
flags: $crate::bindings::VMStateFlags(
diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs
index 99a7aab6fed..d045808c3a8 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -26,7 +26,6 @@
..Zeroable::ZERO
};
-#[derive(qemu_api_macros::offsets)]
#[repr(C)]
#[derive(qemu_api_macros::Object)]
pub struct DummyState {
@@ -79,7 +78,6 @@ fn vmsd() -> Option<&'static VMStateDescription> {
}
}
-#[derive(qemu_api_macros::offsets)]
#[repr(C)]
#[derive(qemu_api_macros::Object)]
pub struct DummyChildState {
diff --git a/rust/qemu-api/tests/vmstate_tests.rs b/rust/qemu-api/tests/vmstate_tests.rs
index 9a56ffd2385..cc3527a9b6a 100644
--- a/rust/qemu-api/tests/vmstate_tests.rs
+++ b/rust/qemu-api/tests/vmstate_tests.rs
@@ -33,7 +33,7 @@
// - VMSTATE_VARRAY_UINT16_UNSAFE
// - VMSTATE_VARRAY_MULTIPLY
#[repr(C)]
-#[derive(Default, qemu_api_macros::offsets)]
+#[derive(Default)]
struct FooA {
arr: [u8; FOO_ARRAY_MAX],
num: u16,
@@ -154,7 +154,7 @@ fn test_vmstate_varray_multiply() {
// - VMSTATE_ARRAY
// - VMSTATE_STRUCT_VARRAY_UINT8 with BqlCell wrapper & test_fn
#[repr(C)]
-#[derive(Default, qemu_api_macros::offsets)]
+#[derive(Default)]
struct FooB {
arr_a: [FooA; FOO_ARRAY_MAX],
num_a: u8,
@@ -329,7 +329,6 @@ fn test_vmstate_struct_varray_uint8_wrapper() {
impl_vmstate_forward!(FooCWrapper);
#[repr(C)]
-#[derive(qemu_api_macros::offsets)]
struct FooC {
ptr: *const i32,
ptr_a: NonNull<FooA>,
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 7/9] rust: replace c_str! with c"" literals
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (5 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 6/9] rust: remove offset_of replacement Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 10:47 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 8/9] rust: enable clippy::ptr_cast_constness Paolo Bonzini
` (2 subsequent siblings)
9 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 8 +---
rust/Cargo.lock | 1 -
rust/hw/char/pl011/src/device_class.rs | 11 +++--
rust/hw/char/pl011/src/lib.rs | 6 +--
rust/hw/timer/hpet/src/hpet.rs | 19 ++++----
rust/hw/timer/hpet/src/lib.rs | 4 +-
rust/qemu-api/meson.build | 1 -
rust/qemu-api/src/c_str.rs | 61 --------------------------
rust/qemu-api/src/cell.rs | 4 +-
rust/qemu-api/src/lib.rs | 1 -
rust/qemu-api/src/vmstate.rs | 2 +-
rust/qemu-api/tests/tests.rs | 9 ++--
rust/qemu-api/tests/vmstate_tests.rs | 15 +++----
13 files changed, 32 insertions(+), 110 deletions(-)
delete mode 100644 rust/qemu-api/src/c_str.rs
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 8dad90fdf98..cae2634a2e9 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -78,14 +78,9 @@ are missing:
* Generic Associated Types (1.65.0)
-* ``CStr::from_bytes_with_nul()`` as a ``const`` function (1.72.0).
-
* "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
the pinned-init create).
-* ``c"" literals`` (stable in 1.77.0). QEMU provides a ``c_str!()`` macro
- to define ``CStr`` constants easily
-
* inline const expression (stable in 1.79.0), currently worked around with
associated constants in the ``FnCall`` trait.
@@ -168,7 +163,6 @@ module status
``bitops`` complete
``callbacks`` complete
``cell`` stable
-``c_str`` complete
``errno`` complete
``irq`` complete
``memory`` stable
@@ -425,7 +419,7 @@ Adding dependencies
Generally, the set of dependent crates is kept small. Think twice before
adding a new external crate, especially if it comes with a large set of
dependencies itself. Sometimes QEMU only needs a small subset of the
-functionality; see for example QEMU's ``assertions`` or ``c_str`` modules.
+functionality; see for example QEMU's ``assertions`` module.
On top of this recommendation, adding external crates to QEMU is a
slightly complicated process, mostly due to the need to teach Meson how
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 2ebf0a11ea4..13d580c693b 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -108,7 +108,6 @@ version = "0.1.0"
dependencies = [
"libc",
"qemu_api_macros",
- "version_check",
]
[[package]]
diff --git a/rust/hw/char/pl011/src/device_class.rs b/rust/hw/char/pl011/src/device_class.rs
index cd2dc33c207..d328d846323 100644
--- a/rust/hw/char/pl011/src/device_class.rs
+++ b/rust/hw/char/pl011/src/device_class.rs
@@ -9,7 +9,6 @@
use qemu_api::{
bindings::{qdev_prop_bool, qdev_prop_chr},
- c_str,
prelude::*,
vmstate::VMStateDescription,
vmstate_clock, vmstate_fields, vmstate_of, vmstate_struct, vmstate_subsections, vmstate_unused,
@@ -25,7 +24,7 @@ extern "C" fn pl011_clock_needed(opaque: *mut c_void) -> bool {
/// Migration subsection for [`PL011State`] clock.
static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription {
- name: c_str!("pl011/clock").as_ptr(),
+ name: c"pl011/clock".as_ptr(),
version_id: 1,
minimum_version_id: 1,
needed: Some(pl011_clock_needed),
@@ -46,7 +45,7 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int {
}
static VMSTATE_PL011_REGS: VMStateDescription = VMStateDescription {
- name: c_str!("pl011/regs").as_ptr(),
+ name: c"pl011/regs".as_ptr(),
version_id: 2,
minimum_version_id: 2,
fields: vmstate_fields! {
@@ -70,7 +69,7 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int {
};
pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
- name: c_str!("pl011").as_ptr(),
+ name: c"pl011".as_ptr(),
version_id: 2,
minimum_version_id: 2,
post_load: Some(pl011_post_load),
@@ -87,14 +86,14 @@ extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int {
qemu_api::declare_properties! {
PL011_PROPERTIES,
qemu_api::define_property!(
- c_str!("chardev"),
+ c"chardev",
PL011State,
char_backend,
unsafe { &qdev_prop_chr },
CharBackend
),
qemu_api::define_property!(
- c_str!("migrate-clk"),
+ c"migrate-clk",
PL011State,
migrate_clock,
unsafe { &qdev_prop_bool },
diff --git a/rust/hw/char/pl011/src/lib.rs b/rust/hw/char/pl011/src/lib.rs
index dbae76991c9..5c4fbc9d148 100644
--- a/rust/hw/char/pl011/src/lib.rs
+++ b/rust/hw/char/pl011/src/lib.rs
@@ -12,13 +12,11 @@
//! See [`PL011State`](crate::device::PL011State) for the device model type and
//! the [`registers`] module for register types.
-use qemu_api::c_str;
-
mod device;
mod device_class;
mod registers;
pub use device::pl011_create;
-pub const TYPE_PL011: &::std::ffi::CStr = c_str!("pl011");
-pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c_str!("pl011_luminary");
+pub const TYPE_PL011: &::std::ffi::CStr = c"pl011";
+pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c"pl011_luminary";
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index 72609c31f44..6afef083a57 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -14,7 +14,6 @@
address_space_memory, address_space_stl_le, qdev_prop_bit, qdev_prop_bool,
qdev_prop_uint32, qdev_prop_uint8,
},
- c_str,
cell::{BqlCell, BqlRefCell},
irq::InterruptSource,
memory::{
@@ -908,7 +907,7 @@ impl ObjectImpl for HPETState {
qemu_api::declare_properties! {
HPET_PROPERTIES,
qemu_api::define_property!(
- c_str!("timers"),
+ c"timers",
HPETState,
num_timers,
unsafe { &qdev_prop_uint8 },
@@ -916,7 +915,7 @@ impl ObjectImpl for HPETState {
default = HPET_MIN_TIMERS
),
qemu_api::define_property!(
- c_str!("msi"),
+ c"msi",
HPETState,
flags,
unsafe { &qdev_prop_bit },
@@ -925,7 +924,7 @@ impl ObjectImpl for HPETState {
default = false,
),
qemu_api::define_property!(
- c_str!("hpet-intcap"),
+ c"hpet-intcap",
HPETState,
int_route_cap,
unsafe { &qdev_prop_uint32 },
@@ -933,7 +932,7 @@ impl ObjectImpl for HPETState {
default = 0
),
qemu_api::define_property!(
- c_str!("hpet-offset-saved"),
+ c"hpet-offset-saved",
HPETState,
hpet_offset_saved,
unsafe { &qdev_prop_bool },
@@ -974,7 +973,7 @@ impl ObjectImpl for HPETState {
}
static VMSTATE_HPET_RTC_IRQ_LEVEL: VMStateDescription = VMStateDescription {
- name: c_str!("hpet/rtc_irq_level").as_ptr(),
+ name: c"hpet/rtc_irq_level".as_ptr(),
version_id: 1,
minimum_version_id: 1,
needed: Some(hpet_rtc_irq_level_needed),
@@ -985,7 +984,7 @@ impl ObjectImpl for HPETState {
};
static VMSTATE_HPET_OFFSET: VMStateDescription = VMStateDescription {
- name: c_str!("hpet/offset").as_ptr(),
+ name: c"hpet/offset".as_ptr(),
version_id: 1,
minimum_version_id: 1,
needed: Some(hpet_offset_needed),
@@ -996,7 +995,7 @@ impl ObjectImpl for HPETState {
};
static VMSTATE_HPET_TIMER: VMStateDescription = VMStateDescription {
- name: c_str!("hpet_timer").as_ptr(),
+ name: c"hpet_timer".as_ptr(),
version_id: 1,
minimum_version_id: 1,
fields: vmstate_fields! {
@@ -1011,10 +1010,10 @@ impl ObjectImpl for HPETState {
..Zeroable::ZERO
};
-const VALIDATE_TIMERS_NAME: &CStr = c_str!("num_timers must match");
+const VALIDATE_TIMERS_NAME: &CStr = c"num_timers must match";
static VMSTATE_HPET: VMStateDescription = VMStateDescription {
- name: c_str!("hpet").as_ptr(),
+ name: c"hpet".as_ptr(),
version_id: 2,
minimum_version_id: 1,
pre_save: Some(hpet_pre_save),
diff --git a/rust/hw/timer/hpet/src/lib.rs b/rust/hw/timer/hpet/src/lib.rs
index 5e7c961c289..1954584a87e 100644
--- a/rust/hw/timer/hpet/src/lib.rs
+++ b/rust/hw/timer/hpet/src/lib.rs
@@ -7,9 +7,7 @@
//! This library implements a device model for the IA-PC HPET (High
//! Precision Event Timers) device in QEMU.
-use qemu_api::c_str;
-
pub mod fw_cfg;
pub mod hpet;
-pub const TYPE_HPET: &::std::ffi::CStr = c_str!("hpet");
+pub const TYPE_HPET: &::std::ffi::CStr = c"hpet";
diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
index 95e65643ae1..1696df705bf 100644
--- a/rust/qemu-api/meson.build
+++ b/rust/qemu-api/meson.build
@@ -20,7 +20,6 @@ _qemu_api_rs = static_library(
'src/callbacks.rs',
'src/cell.rs',
'src/chardev.rs',
- 'src/c_str.rs',
'src/errno.rs',
'src/irq.rs',
'src/memory.rs',
diff --git a/rust/qemu-api/src/c_str.rs b/rust/qemu-api/src/c_str.rs
deleted file mode 100644
index 3fa61b59c76..00000000000
--- a/rust/qemu-api/src/c_str.rs
+++ /dev/null
@@ -1,61 +0,0 @@
-// Copyright 2024 Red Hat, Inc.
-// Author(s): Paolo Bonzini <pbonzini@redhat.com>
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-#![doc(hidden)]
-//! This module provides a macro to define a constant of type
-//! [`CStr`](std::ffi::CStr), for compatibility with versions of
-//! Rust that lack `c""` literals.
-//!
-//! Documentation is hidden because it only exposes macros, which
-//! are exported directly from `qemu_api`.
-
-#[macro_export]
-/// Given a string constant _without_ embedded or trailing NULs, return
-/// a `CStr`.
-///
-/// Needed for compatibility with Rust <1.77.
-macro_rules! c_str {
- ($str:expr) => {{
- const STRING: &str = concat!($str, "\0");
- const BYTES: &[u8] = STRING.as_bytes();
-
- // "for" is not allowed in const context... oh well,
- // everybody loves some lisp. This could be turned into
- // a procedural macro if this is a problem; alternatively
- // Rust 1.72 makes CStr::from_bytes_with_nul a const function.
- const fn f(b: &[u8], i: usize) {
- if i == b.len() - 1 {
- } else if b[i] == 0 {
- panic!("c_str argument contains NUL")
- } else {
- f(b, i + 1)
- }
- }
- f(BYTES, 0);
-
- // SAFETY: absence of NULs apart from the final byte was checked above
- unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(BYTES) }
- }};
-}
-
-#[cfg(test)]
-mod tests {
- use std::ffi::CStr;
-
- use crate::c_str;
-
- #[test]
- fn test_cstr_macro() {
- let good = c_str!("🦀");
- let good_bytes = b"\xf0\x9f\xa6\x80\0";
- assert_eq!(good.to_bytes_with_nul(), good_bytes);
- }
-
- #[test]
- fn test_cstr_macro_const() {
- const GOOD: &CStr = c_str!("🦀");
- const GOOD_BYTES: &[u8] = b"\xf0\x9f\xa6\x80\0";
- assert_eq!(GOOD.to_bytes_with_nul(), GOOD_BYTES);
- }
-}
diff --git a/rust/qemu-api/src/cell.rs b/rust/qemu-api/src/cell.rs
index ab0785a2692..b35d19723f6 100644
--- a/rust/qemu-api/src/cell.rs
+++ b/rust/qemu-api/src/cell.rs
@@ -77,13 +77,13 @@
//!
//! ```
//! # use qemu_api::prelude::*;
-//! # use qemu_api::{c_str, cell::BqlRefCell, irq::InterruptSource, irq::IRQState};
+//! # use qemu_api::{cell::BqlRefCell, irq::InterruptSource, irq::IRQState};
//! # use qemu_api::{sysbus::SysBusDevice, qom::Owned, qom::ParentField};
//! # const N_GPIOS: usize = 8;
//! # struct PL061Registers { /* ... */ }
//! # unsafe impl ObjectType for PL061State {
//! # type Class = <SysBusDevice as ObjectType>::Class;
-//! # const TYPE_NAME: &'static std::ffi::CStr = c_str!("pl061");
+//! # const TYPE_NAME: &'static std::ffi::CStr = c"pl061";
//! # }
//! struct PL061State {
//! parent_obj: ParentField<SysBusDevice>,
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index 90c19636660..234a94e3c29 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -15,7 +15,6 @@
pub mod assertions;
pub mod bitops;
-pub mod c_str;
pub mod callbacks;
pub mod cell;
pub mod chardev;
diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
index 9a6e6b734b9..389f1a6f62a 100644
--- a/rust/qemu-api/src/vmstate.rs
+++ b/rust/qemu-api/src/vmstate.rs
@@ -426,7 +426,7 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* {
macro_rules! vmstate_unused {
($size:expr) => {{
$crate::bindings::VMStateField {
- name: $crate::c_str!("unused").as_ptr(),
+ name: c"unused".as_ptr(),
size: $size,
info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) },
flags: $crate::bindings::VMStateFlags::VMS_BUFFER,
diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs
index d045808c3a8..a658a49fcfd 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -6,7 +6,6 @@
use qemu_api::{
bindings::{module_call_init, module_init_type, qdev_prop_bool},
- c_str,
cell::{self, BqlCell},
declare_properties, define_property,
prelude::*,
@@ -21,7 +20,7 @@
// Test that macros can compile.
pub static VMSTATE: VMStateDescription = VMStateDescription {
- name: c_str!("name").as_ptr(),
+ name: c"name".as_ptr(),
unmigratable: true,
..Zeroable::ZERO
};
@@ -48,7 +47,7 @@ pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) {
declare_properties! {
DUMMY_PROPERTIES,
define_property!(
- c_str!("migrate-clk"),
+ c"migrate-clk",
DummyState,
migrate_clock,
unsafe { &qdev_prop_bool },
@@ -58,7 +57,7 @@ pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) {
unsafe impl ObjectType for DummyState {
type Class = DummyClass;
- const TYPE_NAME: &'static CStr = c_str!("dummy");
+ const TYPE_NAME: &'static CStr = c"dummy";
}
impl ObjectImpl for DummyState {
@@ -92,7 +91,7 @@ pub struct DummyChildClass {
unsafe impl ObjectType for DummyChildState {
type Class = DummyChildClass;
- const TYPE_NAME: &'static CStr = c_str!("dummy_child");
+ const TYPE_NAME: &'static CStr = c"dummy_child";
}
impl ObjectImpl for DummyChildState {
diff --git a/rust/qemu-api/tests/vmstate_tests.rs b/rust/qemu-api/tests/vmstate_tests.rs
index cc3527a9b6a..ad0fc5cd5dd 100644
--- a/rust/qemu-api/tests/vmstate_tests.rs
+++ b/rust/qemu-api/tests/vmstate_tests.rs
@@ -14,7 +14,6 @@
vmstate_info_bool, vmstate_info_int32, vmstate_info_int64, vmstate_info_int8,
vmstate_info_uint64, vmstate_info_uint8, vmstate_info_unused_buffer, VMStateFlags,
},
- c_str,
cell::{BqlCell, Opaque},
impl_vmstate_forward,
vmstate::{VMStateDescription, VMStateField},
@@ -43,7 +42,7 @@ struct FooA {
}
static VMSTATE_FOOA: VMStateDescription = VMStateDescription {
- name: c_str!("foo_a").as_ptr(),
+ name: c"foo_a".as_ptr(),
version_id: 1,
minimum_version_id: 1,
fields: vmstate_fields! {
@@ -173,7 +172,7 @@ fn validate_foob(_state: &FooB, _version_id: u8) -> bool {
}
static VMSTATE_FOOB: VMStateDescription = VMStateDescription {
- name: c_str!("foo_b").as_ptr(),
+ name: c"foo_b".as_ptr(),
version_id: 2,
minimum_version_id: 1,
fields: vmstate_fields! {
@@ -337,7 +336,7 @@ struct FooC {
}
static VMSTATE_FOOC: VMStateDescription = VMStateDescription {
- name: c_str!("foo_c").as_ptr(),
+ name: c"foo_c".as_ptr(),
version_id: 3,
minimum_version_id: 1,
fields: vmstate_fields! {
@@ -452,13 +451,13 @@ fn validate_food_2(_state: &FooD, _version_id: u8) -> bool {
}
static VMSTATE_FOOD: VMStateDescription = VMStateDescription {
- name: c_str!("foo_d").as_ptr(),
+ name: c"foo_d".as_ptr(),
version_id: 3,
minimum_version_id: 1,
fields: vmstate_fields! {
- vmstate_validate!(FooD, c_str!("foo_d_0"), FooD::validate_food_0),
- vmstate_validate!(FooD, c_str!("foo_d_1"), FooD::validate_food_1),
- vmstate_validate!(FooD, c_str!("foo_d_2"), validate_food_2),
+ vmstate_validate!(FooD, c"foo_d_0", FooD::validate_food_0),
+ vmstate_validate!(FooD, c"foo_d_1", FooD::validate_food_1),
+ vmstate_validate!(FooD, c"foo_d_2", validate_food_2),
},
..Zeroable::ZERO
};
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 8/9] rust: enable clippy::ptr_cast_constness
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (6 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 7/9] rust: replace c_str! with c"" literals Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 11:09 ` Manos Pitsidianakis
2025-05-02 18:57 ` Stefan Zabka
2025-05-02 10:23 ` [PATCH 9/9] docs: rust: update for newer minimum supported version Paolo Bonzini
2025-05-02 10:54 ` [PATCH 0/9] rust: allow minimum version of 1.77 Manos Pitsidianakis
9 siblings, 2 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 2 --
rust/Cargo.toml | 2 +-
rust/clippy.toml | 2 +-
rust/hw/char/pl011/Cargo.toml | 2 +-
rust/hw/timer/hpet/Cargo.toml | 2 +-
rust/hw/timer/hpet/src/hpet.rs | 2 +-
rust/qemu-api-macros/Cargo.toml | 2 +-
rust/qemu-api/Cargo.toml | 2 +-
rust/qemu-api/src/cell.rs | 2 +-
rust/qemu-api/src/chardev.rs | 2 +-
rust/qemu-api/src/qom.rs | 4 ++--
rust/qemu-api/src/timer.rs | 2 +-
12 files changed, 12 insertions(+), 14 deletions(-)
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index cae2634a2e9..92550d9d581 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -74,8 +74,6 @@ Supported tools
QEMU supports rustc version 1.63.0 and newer. Notably, the following features
are missing:
-* ``cast_mut()``/``cast_const()`` (1.65.0). Use ``as`` instead.
-
* Generic Associated Types (1.65.0)
* "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index ea2a85a1d07..bcf43718cf9 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -64,6 +64,7 @@ no_effect_underscore_binding = "deny"
option_option = "deny"
or_fun_call = "deny"
ptr_as_ptr = "deny"
+ptr_cast_constness = "deny"
pub_underscore_fields = "deny"
redundant_clone = "deny"
redundant_closure_for_method_calls = "deny"
@@ -85,7 +86,6 @@ used_underscore_binding = "deny"
# nice to have, but cannot be enabled yet
#wildcard_imports = "deny" # still have many bindings::* imports
-#ptr_cast_constness = "deny" # needs 1.65.0 for cast_mut()/cast_const()
# these may have false positives
#option_if_let_else = "deny"
diff --git a/rust/clippy.toml b/rust/clippy.toml
index 5d190f91dec..933e46a2ffb 100644
--- a/rust/clippy.toml
+++ b/rust/clippy.toml
@@ -1,2 +1,2 @@
doc-valid-idents = ["PrimeCell", ".."]
-msrv = "1.63.0"
+msrv = "1.77.0"
diff --git a/rust/hw/char/pl011/Cargo.toml b/rust/hw/char/pl011/Cargo.toml
index f2296cad58b..4766badd73e 100644
--- a/rust/hw/char/pl011/Cargo.toml
+++ b/rust/hw/char/pl011/Cargo.toml
@@ -9,7 +9,7 @@ resolver = "2"
publish = false
keywords = []
categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
[lib]
crate-type = ["staticlib"]
diff --git a/rust/hw/timer/hpet/Cargo.toml b/rust/hw/timer/hpet/Cargo.toml
index 147f216e725..c6c48b2f887 100644
--- a/rust/hw/timer/hpet/Cargo.toml
+++ b/rust/hw/timer/hpet/Cargo.toml
@@ -5,7 +5,7 @@ edition = "2021"
authors = ["Zhao Liu <zhao1.liu@intel.com>"]
license = "GPL-2.0-or-later"
description = "IA-PC High Precision Event Timer emulation in Rust"
-rust-version = "1.63.0"
+rust-version = "1.77.0"
[lib]
crate-type = ["staticlib"]
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index 6afef083a57..779681d6509 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -218,7 +218,7 @@ fn init(&mut self, index: u8, state: &HPETState) {
// SAFETY: the HPETTimer will only be used after the timer
// is initialized below.
qemu_timer: unsafe { Timer::new() },
- state: NonNull::new(state as *const _ as *mut _).unwrap(),
+ state: NonNull::new((state as *const HPETState).cast_mut()).unwrap(),
config: 0,
cmp: 0,
fsb: 0,
diff --git a/rust/qemu-api-macros/Cargo.toml b/rust/qemu-api-macros/Cargo.toml
index 89dee1cfb39..8344b1f939d 100644
--- a/rust/qemu-api-macros/Cargo.toml
+++ b/rust/qemu-api-macros/Cargo.toml
@@ -9,7 +9,7 @@ resolver = "2"
publish = false
keywords = []
categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
[lib]
proc-macro = true
diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
index c5b7b2464e9..be00ef1ae9b 100644
--- a/rust/qemu-api/Cargo.toml
+++ b/rust/qemu-api/Cargo.toml
@@ -12,7 +12,7 @@ resolver = "2"
publish = false
keywords = []
categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
[dependencies]
qemu_api_macros = { path = "../qemu-api-macros" }
diff --git a/rust/qemu-api/src/cell.rs b/rust/qemu-api/src/cell.rs
index b35d19723f6..05ce09f6cb6 100644
--- a/rust/qemu-api/src/cell.rs
+++ b/rust/qemu-api/src/cell.rs
@@ -1016,7 +1016,7 @@ pub const fn as_mut_ptr(&self) -> *mut T {
/// Returns a raw pointer to the opaque data.
pub const fn as_ptr(&self) -> *const T {
- self.as_mut_ptr() as *const _
+ self.as_mut_ptr().cast_const()
}
/// Returns a raw pointer to the opaque data that can be passed to a
diff --git a/rust/qemu-api/src/chardev.rs b/rust/qemu-api/src/chardev.rs
index 146a4852da3..6e0590d758e 100644
--- a/rust/qemu-api/src/chardev.rs
+++ b/rust/qemu-api/src/chardev.rs
@@ -160,7 +160,7 @@ pub fn enable_handlers<
receive_cb,
event_cb,
None,
- (owner as *const T as *mut T).cast::<c_void>(),
+ (owner as *const T).cast_mut().cast::<c_void>(),
core::ptr::null_mut(),
true,
);
diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs
index 6929e4d33ae..9766a607e73 100644
--- a/rust/qemu-api/src/qom.rs
+++ b/rust/qemu-api/src/qom.rs
@@ -388,7 +388,7 @@ fn as_mut_ptr<U: ObjectType>(&self) -> *mut U
{
#[allow(clippy::as_ptr_cast_mut)]
{
- self.as_ptr::<U>() as *mut _
+ self.as_ptr::<U>().cast_mut()
}
}
}
@@ -637,7 +637,7 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
// SAFETY NOTE: while NonNull requires a mutable pointer, only
// Deref is implemented so the pointer passed to from_raw
// remains const
- Owned(NonNull::new(ptr as *mut T).unwrap())
+ Owned(NonNull::new(ptr.cast_mut()).unwrap())
}
/// Obtain a raw C pointer from a reference. `src` is consumed
diff --git a/rust/qemu-api/src/timer.rs b/rust/qemu-api/src/timer.rs
index d697fd742bc..868bd88575f 100644
--- a/rust/qemu-api/src/timer.rs
+++ b/rust/qemu-api/src/timer.rs
@@ -81,7 +81,7 @@ pub fn init_full<'timer, 'opaque: 'timer, T, F>(
scale as c_int,
attributes as c_int,
Some(timer_cb),
- (opaque as *const T).cast::<c_void>() as *mut c_void,
+ (opaque as *const T).cast::<c_void>().cast_mut(),
)
}
}
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* [PATCH 9/9] docs: rust: update for newer minimum supported version
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (7 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 8/9] rust: enable clippy::ptr_cast_constness Paolo Bonzini
@ 2025-05-02 10:23 ` Paolo Bonzini
2025-05-02 10:54 ` [PATCH 0/9] rust: allow minimum version of 1.77 Manos Pitsidianakis
9 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:23 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust
Remove leftover notes for Rust changes between 1.63.0 and 1.77.0.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 92550d9d581..4de86375021 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -71,14 +71,9 @@ Building Rust code with ``--enable-modules`` is not supported yet.
Supported tools
'''''''''''''''
-QEMU supports rustc version 1.63.0 and newer. Notably, the following features
+QEMU supports rustc version 1.77.0 and newer. Notably, the following features
are missing:
-* Generic Associated Types (1.65.0)
-
-* "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
- the pinned-init create).
-
* inline const expression (stable in 1.79.0), currently worked around with
associated constants in the ``FnCall`` trait.
@@ -103,12 +98,6 @@ are missing:
__ https://github.com/rust-lang/rust/pull/125258
-It is expected that QEMU will advance its minimum supported version of
-rustc to 1.77.0 as soon as possible; as of January 2025, blockers
-for that right now are Debian bookworm and 32-bit MIPS processors.
-This unfortunately means that references to statics in constants will
-remain an issue.
-
QEMU also supports version 0.60.x of bindgen, which is missing option
``--generate-cstr``. This option requires version 0.66.x and will
be adopted as soon as supporting these older versions is not necessary
--
2.49.0
^ permalink raw reply related [flat|nested] 23+ messages in thread
* Re: [PATCH 7/9] rust: replace c_str! with c"" literals
2025-05-02 10:23 ` [PATCH 7/9] rust: replace c_str! with c"" literals Paolo Bonzini
@ 2025-05-02 10:47 ` Manos Pitsidianakis
2025-05-02 10:53 ` Paolo Bonzini
0 siblings, 1 reply; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 10:47 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst | 8 +---
> rust/Cargo.lock | 1 -
> rust/hw/char/pl011/src/device_class.rs | 11 +++--
> rust/hw/char/pl011/src/lib.rs | 6 +--
> rust/hw/timer/hpet/src/hpet.rs | 19 ++++----
> rust/hw/timer/hpet/src/lib.rs | 4 +-
> rust/qemu-api/meson.build | 1 -
> rust/qemu-api/src/c_str.rs | 61 --------------------------
> rust/qemu-api/src/cell.rs | 4 +-
> rust/qemu-api/src/lib.rs | 1 -
> rust/qemu-api/src/vmstate.rs | 2 +-
> rust/qemu-api/tests/tests.rs | 9 ++--
> rust/qemu-api/tests/vmstate_tests.rs | 15 +++----
> 13 files changed, 32 insertions(+), 110 deletions(-)
> delete mode 100644 rust/qemu-api/src/c_str.rs
>
<-snip->
>diff --git a/rust/Cargo.lock b/rust/Cargo.lock
>index 2ebf0a11ea4..13d580c693b 100644
>--- a/rust/Cargo.lock
>+++ b/rust/Cargo.lock
>@@ -108,7 +108,6 @@ version = "0.1.0"
> dependencies = [
> "libc",
> "qemu_api_macros",
>- "version_check",
> ]
This looks like it should go in the previous patch ("rust: remove
offset_of replacement")
Otherwise,
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 7/9] rust: replace c_str! with c"" literals
2025-05-02 10:47 ` Manos Pitsidianakis
@ 2025-05-02 10:53 ` Paolo Bonzini
0 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 10:53 UTC (permalink / raw)
To: Manos Pitsidianakis; +Cc: qemu-devel, qemu-rust
On Fri, May 2, 2025 at 12:52 PM Manos Pitsidianakis
<manos.pitsidianakis@linaro.org> wrote:
> This looks like it should go in the previous patch ("rust: remove
> offset_of replacement")
>
> Otherwise,
>
> Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
You're right! Thanks for the review, and welcome back.
Paolo
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 0/9] rust: allow minimum version of 1.77
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
` (8 preceding siblings ...)
2025-05-02 10:23 ` [PATCH 9/9] docs: rust: update for newer minimum supported version Paolo Bonzini
@ 2025-05-02 10:54 ` Manos Pitsidianakis
9 siblings, 0 replies; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 10:54 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
Hey Paolo,
We should update the version check in the root meson.build in the first
patch of this series:
> if have_rust
> rustc = meson.get_compiler('rust')
> if rustc.version().version_compare('<1.63.0')
> if get_option('rust').enabled()
> error('rustc version ' + rustc.version() + ' is unsupported. Please upgrade to at least 1.63.0')
> else
> warning('rustc version ' + rustc.version() + ' is unsupported, disabling Rust compilation.')
> message('Please upgrade to at least 1.63.0 to use Rust.')
> have_rust = false
> endif
> endif
> endif
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 2/9] rust: use std::ffi instead of std::os::raw
2025-05-02 10:23 ` [PATCH 2/9] rust: use std::ffi instead of std::os::raw Paolo Bonzini
@ 2025-05-02 10:57 ` Manos Pitsidianakis
0 siblings, 0 replies; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 10:57 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>This is allowed since Rust 1.64.0.
>
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst | 2 --
> rust/hw/char/pl011/src/device_class.rs | 2 +-
> rust/hw/timer/hpet/src/hpet.rs | 3 +--
> rust/qemu-api/src/chardev.rs | 3 +--
> rust/qemu-api/src/irq.rs | 6 +++++-
> rust/qemu-api/src/lib.rs | 2 +-
> rust/qemu-api/src/memory.rs | 3 +--
> rust/qemu-api/src/qdev.rs | 3 +--
> rust/qemu-api/src/qom.rs | 3 +--
> rust/qemu-api/src/timer.rs | 2 +-
> rust/qemu-api/src/vmstate.rs | 2 +-
> rust/qemu-api/tests/vmstate_tests.rs | 7 ++++++-
> 12 files changed, 20 insertions(+), 18 deletions(-)
>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context
2025-05-02 10:23 ` [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
@ 2025-05-02 11:01 ` Manos Pitsidianakis
2025-05-02 12:22 ` Paolo Bonzini
0 siblings, 1 reply; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 11:01 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst | 4 --
> rust/hw/timer/hpet/src/fw_cfg.rs | 6 +-
> rust/qemu-api/src/zeroable.rs | 104 +++++--------------------------
> 3 files changed, 18 insertions(+), 96 deletions(-)
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
BTW There's this TODO in qom.rs, ObjectImpl trait
> /// `&mut T`. TODO: add a bound of
> //[`Zeroable`](crate::zeroable::Zeroable)
> /// to T; this is more easily done once Zeroable does not require a manual
> /// implementation (Rust 1.75.0).
> const CLASS_INIT: fn(&mut Self::Class);
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
2025-05-02 10:23 ` [PATCH 8/9] rust: enable clippy::ptr_cast_constness Paolo Bonzini
@ 2025-05-02 11:09 ` Manos Pitsidianakis
2025-05-02 12:28 ` Paolo Bonzini
2025-05-02 18:57 ` Stefan Zabka
1 sibling, 1 reply; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 11:09 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst | 2 --
> rust/Cargo.toml | 2 +-
> rust/clippy.toml | 2 +-
> rust/hw/char/pl011/Cargo.toml | 2 +-
> rust/hw/timer/hpet/Cargo.toml | 2 +-
> rust/hw/timer/hpet/src/hpet.rs | 2 +-
> rust/qemu-api-macros/Cargo.toml | 2 +-
> rust/qemu-api/Cargo.toml | 2 +-
> rust/qemu-api/src/cell.rs | 2 +-
> rust/qemu-api/src/chardev.rs | 2 +-
> rust/qemu-api/src/qom.rs | 4 ++--
> rust/qemu-api/src/timer.rs | 2 +-
> 12 files changed, 12 insertions(+), 14 deletions(-)
>
These:
>diff --git a/rust/clippy.toml b/rust/clippy.toml
>index 5d190f91dec..933e46a2ffb 100644
>--- a/rust/clippy.toml
>+++ b/rust/clippy.toml
>@@ -1,2 +1,2 @@
> doc-valid-idents = ["PrimeCell", ".."]
>-msrv = "1.63.0"
>+msrv = "1.77.0"
>diff --git a/rust/hw/char/pl011/Cargo.toml b/rust/hw/char/pl011/Cargo.toml
>index f2296cad58b..4766badd73e 100644
>--- a/rust/hw/char/pl011/Cargo.toml
>+++ b/rust/hw/char/pl011/Cargo.toml
>@@ -9,7 +9,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
>
> [lib]
> crate-type = ["staticlib"]
>diff --git a/rust/hw/timer/hpet/Cargo.toml b/rust/hw/timer/hpet/Cargo.toml
>index 147f216e725..c6c48b2f887 100644
>--- a/rust/hw/timer/hpet/Cargo.toml
>+++ b/rust/hw/timer/hpet/Cargo.toml
>@@ -5,7 +5,7 @@ edition = "2021"
> authors = ["Zhao Liu <zhao1.liu@intel.com>"]
> license = "GPL-2.0-or-later"
> description = "IA-PC High Precision Event Timer emulation in Rust"
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
>
> [lib]
> crate-type = ["staticlib"]
>diff --git a/rust/qemu-api-macros/Cargo.toml
>b/rust/qemu-api-macros/Cargo.toml
>index 89dee1cfb39..8344b1f939d 100644
>--- a/rust/qemu-api-macros/Cargo.toml
>+++ b/rust/qemu-api-macros/Cargo.toml
>@@ -9,7 +9,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
>
> [lib]
> proc-macro = true
>diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
>index c5b7b2464e9..be00ef1ae9b 100644
>--- a/rust/qemu-api/Cargo.toml
>+++ b/rust/qemu-api/Cargo.toml
>@@ -12,7 +12,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
>
> [dependencies]
> qemu_api_macros = { path = "../qemu-api-macros" }
msrv/rust-version bumps could instead go in the patch that also updates
meson.build version.
Otherwise,
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context
2025-05-02 11:01 ` Manos Pitsidianakis
@ 2025-05-02 12:22 ` Paolo Bonzini
2025-05-02 13:05 ` Manos Pitsidianakis
0 siblings, 1 reply; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 12:22 UTC (permalink / raw)
To: Manos Pitsidianakis, qemu-devel; +Cc: qemu-rust
On 5/2/25 13:01, Manos Pitsidianakis wrote:
> On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>> docs/devel/rust.rst | 4 --
>> rust/hw/timer/hpet/src/fw_cfg.rs | 6 +-
>> rust/qemu-api/src/zeroable.rs | 104 +++++--------------------------
>> 3 files changed, 18 insertions(+), 96 deletions(-)
>
> Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
>
> BTW There's this TODO in qom.rs, ObjectImpl trait
>
>> /// `&mut T`. TODO: add a bound of
>> //[`Zeroable`](crate::zeroable::Zeroable)
>> /// to T; this is more easily done once Zeroable does not require a manual
>> /// implementation (Rust 1.75.0).
>> const CLASS_INIT: fn(&mut Self::Class);
Yes, good point. When I wrote the TODO, my idea here was to have some
kind of
#[derive(Zeroable)]
macro so that the compiler can "confirm" the safety of implementing
Zeroable by hand.
However, most of the time the class will be just a C-defined class
(DeviceClass or SysBusDeviceClass), and then it's not even possible to
add the derive attribute to the declaration. So adding the bound to
ObjectType::Class is feasible now that one can just add
unsafe impl Zeroable for bindings::ObjectClass {}
unsafe impl Zeroable for bindings::DeviceClass {}
unsafe impl Zeroable for bindings::SysBusDeviceClass {}
unsafe impl Zeroable for bindings::ResettableClass {}
etc.; in fact it was already possible when Zhao added the impl_zeroed!
macro in commit aaf3778baaa ("rust/zeroable: Implement Zeroable with
const_zero macro", 2025-01-28).
Paolo
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
2025-05-02 11:09 ` Manos Pitsidianakis
@ 2025-05-02 12:28 ` Paolo Bonzini
0 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 12:28 UTC (permalink / raw)
To: Manos Pitsidianakis; +Cc: qemu-devel, qemu-rust
On Fri, May 2, 2025 at 1:13 PM Manos Pitsidianakis
<manos.pitsidianakis@linaro.org> wrote:
> msrv/rust-version bumps could instead go in the patch that also updates
> meson.build version.
Good idea, I was mistaken that ptr_cast_constness was already enabled
by bumping them, but it's actually default-allow.
Paolo
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 3/9] rust: let bilge use "let ... else"
2025-05-02 10:23 ` [PATCH 3/9] rust: let bilge use "let ... else" Paolo Bonzini
@ 2025-05-02 12:44 ` Manos Pitsidianakis
0 siblings, 0 replies; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 12:44 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst | 3 --
> subprojects/bilge-impl-0.2-rs.wrap | 2 -
> .../packagefiles/bilge-impl-1.63.0.patch | 45 -------------------
> 3 files changed, 50 deletions(-)
> delete mode 100644 subprojects/packagefiles/bilge-impl-1.63.0.patch
>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 4/9] rust: qemu_api_macros: use "let ... else"
2025-05-02 10:23 ` [PATCH 4/9] rust: qemu_api_macros: " Paolo Bonzini
@ 2025-05-02 12:45 ` Manos Pitsidianakis
0 siblings, 0 replies; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 12:45 UTC (permalink / raw)
To: qemu-devel, Paolo Bonzini; +Cc: qemu-rust
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>"let ... else" is useful when visiting syntax trees, to avoid multiple
>levels of indentation.
>
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> rust/qemu-api-macros/src/lib.rs | 84 ++++++++++++++++-----------------
> 1 file changed, 40 insertions(+), 44 deletions(-)
>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context
2025-05-02 12:22 ` Paolo Bonzini
@ 2025-05-02 13:05 ` Manos Pitsidianakis
0 siblings, 0 replies; 23+ messages in thread
From: Manos Pitsidianakis @ 2025-05-02 13:05 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: qemu-devel, qemu-rust
On Fri, May 2, 2025 at 3:23 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 5/2/25 13:01, Manos Pitsidianakis wrote:
> > On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
> >> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> >> ---
> >> docs/devel/rust.rst | 4 --
> >> rust/hw/timer/hpet/src/fw_cfg.rs | 6 +-
> >> rust/qemu-api/src/zeroable.rs | 104 +++++--------------------------
> >> 3 files changed, 18 insertions(+), 96 deletions(-)
> >
> > Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> >
> > BTW There's this TODO in qom.rs, ObjectImpl trait
> >
> >> /// `&mut T`. TODO: add a bound of
> >> //[`Zeroable`](crate::zeroable::Zeroable)
> >> /// to T; this is more easily done once Zeroable does not require a manual
> >> /// implementation (Rust 1.75.0).
> >> const CLASS_INIT: fn(&mut Self::Class);
>
> Yes, good point. When I wrote the TODO, my idea here was to have some
> kind of
>
> #[derive(Zeroable)]
>
> macro so that the compiler can "confirm" the safety of implementing
> Zeroable by hand.
>
> However, most of the time the class will be just a C-defined class
> (DeviceClass or SysBusDeviceClass), and then it's not even possible to
> add the derive attribute to the declaration.
Yes this makes sense. A benefit of having `#[derive(Zeroable)]` is
avoiding blanket unsafe impls of the trait, since the derive macro
would work only if all fields implement Zeroable (so in a sense, it's
safe as long as the individual field type impls are safe,
transitively). And we can still do manual unsafe impls for everything
from the FFI boundary.
> So adding the bound to
> ObjectType::Class is feasible now that one can just add
>
> unsafe impl Zeroable for bindings::ObjectClass {}
> unsafe impl Zeroable for bindings::DeviceClass {}
> unsafe impl Zeroable for bindings::SysBusDeviceClass {}
> unsafe impl Zeroable for bindings::ResettableClass {}
>
> etc.; in fact it was already possible when Zhao added the impl_zeroed!
> macro in commit aaf3778baaa ("rust/zeroable: Implement Zeroable with
> const_zero macro", 2025-01-28).
>
> Paolo
>
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
2025-05-02 10:23 ` [PATCH 8/9] rust: enable clippy::ptr_cast_constness Paolo Bonzini
2025-05-02 11:09 ` Manos Pitsidianakis
@ 2025-05-02 18:57 ` Stefan Zabka
2025-05-02 19:32 ` Paolo Bonzini
1 sibling, 1 reply; 23+ messages in thread
From: Stefan Zabka @ 2025-05-02 18:57 UTC (permalink / raw)
To: Paolo Bonzini, qemu-devel; +Cc: qemu-rust
Unrelated to the actual change, but a drive-by observation:
If you want to keep the MSRV in sync across all workspace members, you
could use workspace.package.rust-version to define it once and inherit
it everywhere. (Same goes for edition, license, and resolver.)
https://doc.rust-lang.org/cargo/reference/workspaces.html#the-package-table
I'd like to provide a patch instead of just heckling, but I have a
couple of questions:
1. Is this actually desired behaviour, or might you want different
values for these fields?
2. Should I implement this against qemu-project/qemu master branch or is
there a dedicated Rust tree?
3. Should I wait with that until your change is landed so we don't conflict?
Best
Stefan
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
2025-05-02 18:57 ` Stefan Zabka
@ 2025-05-02 19:32 ` Paolo Bonzini
0 siblings, 0 replies; 23+ messages in thread
From: Paolo Bonzini @ 2025-05-02 19:32 UTC (permalink / raw)
To: Stefan Zabka; +Cc: qemu-devel, qemu-rust
[-- Attachment #1: Type: text/plain, Size: 910 bytes --]
Il ven 2 mag 2025, 20:58 Stefan Zabka <git@zabka.it> ha scritto:
> Unrelated to the actual change, but a drive-by observation:
> If you want to keep the MSRV in sync across all workspace members, you
> could use workspace.package.rust-version to define it once and inherit
> it everywhere. (Same goes for edition, license, and resolver.)
>
> https://doc.rust-lang.org/cargo/reference/workspaces.html#the-package-table
>
> I'd like to provide a patch instead of just heckling, but I have a
> couple of questions:
>
> 1. Is this actually desired behaviour, or might you want different
> values for these fields?
>
Yes, it is.
2. Should I implement this against qemu-project/qemu master branch or is
> there a dedicated Rust tree?
>
Sure, go ahead! I will rebase on top, since I have to resend anyway.
Paolo
3. Should I wait with that until your change is landed so we don't conflict?
>
> Best
> Stefan
>
>
[-- Attachment #2: Type: text/html, Size: 2035 bytes --]
^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2025-05-02 19:33 UTC | newest]
Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-02 10:23 [PATCH 0/9] rust: allow minimum version of 1.77 Paolo Bonzini
2025-05-02 10:23 ` [PATCH 1/9] lcitool: use Rust 1.78 for Debian bookworm Paolo Bonzini
2025-05-02 10:23 ` [PATCH 2/9] rust: use std::ffi instead of std::os::raw Paolo Bonzini
2025-05-02 10:57 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 3/9] rust: let bilge use "let ... else" Paolo Bonzini
2025-05-02 12:44 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 4/9] rust: qemu_api_macros: " Paolo Bonzini
2025-05-02 12:45 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 5/9] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
2025-05-02 11:01 ` Manos Pitsidianakis
2025-05-02 12:22 ` Paolo Bonzini
2025-05-02 13:05 ` Manos Pitsidianakis
2025-05-02 10:23 ` [PATCH 6/9] rust: remove offset_of replacement Paolo Bonzini
2025-05-02 10:23 ` [PATCH 7/9] rust: replace c_str! with c"" literals Paolo Bonzini
2025-05-02 10:47 ` Manos Pitsidianakis
2025-05-02 10:53 ` Paolo Bonzini
2025-05-02 10:23 ` [PATCH 8/9] rust: enable clippy::ptr_cast_constness Paolo Bonzini
2025-05-02 11:09 ` Manos Pitsidianakis
2025-05-02 12:28 ` Paolo Bonzini
2025-05-02 18:57 ` Stefan Zabka
2025-05-02 19:32 ` Paolo Bonzini
2025-05-02 10:23 ` [PATCH 9/9] docs: rust: update for newer minimum supported version Paolo Bonzini
2025-05-02 10:54 ` [PATCH 0/9] rust: allow minimum version of 1.77 Manos Pitsidianakis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).