rust: replace c_str! with c"" literals

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2025-05-02 10:39:08 +02:00
parent b134a09ffa
commit f117857b39
12 changed files with 32 additions and 107 deletions

View file

@ -81,9 +81,6 @@ are missing:
* "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including * "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
the pinned-init create). 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 * inline const expression (stable in 1.79.0), currently worked around with
associated constants in the ``FnCall`` trait. associated constants in the ``FnCall`` trait.
@ -166,7 +163,6 @@ module status
``bitops`` complete ``bitops`` complete
``callbacks`` complete ``callbacks`` complete
``cell`` stable ``cell`` stable
``c_str`` complete
``errno`` complete ``errno`` complete
``irq`` complete ``irq`` complete
``memory`` stable ``memory`` stable
@ -423,7 +419,7 @@ Adding dependencies
Generally, the set of dependent crates is kept small. Think twice before 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 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 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 On top of this recommendation, adding external crates to QEMU is a
slightly complicated process, mostly due to the need to teach Meson how slightly complicated process, mostly due to the need to teach Meson how

View file

@ -9,7 +9,6 @@ use std::{
use qemu_api::{ use qemu_api::{
bindings::{qdev_prop_bool, qdev_prop_chr}, bindings::{qdev_prop_bool, qdev_prop_chr},
c_str,
prelude::*, prelude::*,
vmstate::VMStateDescription, vmstate::VMStateDescription,
vmstate_clock, vmstate_fields, vmstate_of, vmstate_struct, vmstate_subsections, vmstate_unused, 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. /// Migration subsection for [`PL011State`] clock.
static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription { static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription {
name: c_str!("pl011/clock").as_ptr(), name: c"pl011/clock".as_ptr(),
version_id: 1, version_id: 1,
minimum_version_id: 1, minimum_version_id: 1,
needed: Some(pl011_clock_needed), 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 { static VMSTATE_PL011_REGS: VMStateDescription = VMStateDescription {
name: c_str!("pl011/regs").as_ptr(), name: c"pl011/regs".as_ptr(),
version_id: 2, version_id: 2,
minimum_version_id: 2, minimum_version_id: 2,
fields: vmstate_fields! { fields: vmstate_fields! {
@ -70,7 +69,7 @@ static VMSTATE_PL011_REGS: VMStateDescription = VMStateDescription {
}; };
pub static VMSTATE_PL011: VMStateDescription = VMStateDescription { pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
name: c_str!("pl011").as_ptr(), name: c"pl011".as_ptr(),
version_id: 2, version_id: 2,
minimum_version_id: 2, minimum_version_id: 2,
post_load: Some(pl011_post_load), post_load: Some(pl011_post_load),
@ -87,14 +86,14 @@ pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
qemu_api::declare_properties! { qemu_api::declare_properties! {
PL011_PROPERTIES, PL011_PROPERTIES,
qemu_api::define_property!( qemu_api::define_property!(
c_str!("chardev"), c"chardev",
PL011State, PL011State,
char_backend, char_backend,
unsafe { &qdev_prop_chr }, unsafe { &qdev_prop_chr },
CharBackend CharBackend
), ),
qemu_api::define_property!( qemu_api::define_property!(
c_str!("migrate-clk"), c"migrate-clk",
PL011State, PL011State,
migrate_clock, migrate_clock,
unsafe { &qdev_prop_bool }, unsafe { &qdev_prop_bool },

View file

@ -12,13 +12,11 @@
//! See [`PL011State`](crate::device::PL011State) for the device model type and //! See [`PL011State`](crate::device::PL011State) for the device model type and
//! the [`registers`] module for register types. //! the [`registers`] module for register types.
use qemu_api::c_str;
mod device; mod device;
mod device_class; mod device_class;
mod registers; mod registers;
pub use device::pl011_create; pub use device::pl011_create;
pub const TYPE_PL011: &::std::ffi::CStr = c_str!("pl011"); pub const TYPE_PL011: &::std::ffi::CStr = c"pl011";
pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c_str!("pl011_luminary"); pub const TYPE_PL011_LUMINARY: &::std::ffi::CStr = c"pl011_luminary";

View file

@ -14,7 +14,6 @@ use qemu_api::{
address_space_memory, address_space_stl_le, qdev_prop_bit, qdev_prop_bool, address_space_memory, address_space_stl_le, qdev_prop_bit, qdev_prop_bool,
qdev_prop_uint32, qdev_prop_uint8, qdev_prop_uint32, qdev_prop_uint8,
}, },
c_str,
cell::{BqlCell, BqlRefCell}, cell::{BqlCell, BqlRefCell},
irq::InterruptSource, irq::InterruptSource,
memory::{ memory::{
@ -908,7 +907,7 @@ impl ObjectImpl for HPETState {
qemu_api::declare_properties! { qemu_api::declare_properties! {
HPET_PROPERTIES, HPET_PROPERTIES,
qemu_api::define_property!( qemu_api::define_property!(
c_str!("timers"), c"timers",
HPETState, HPETState,
num_timers, num_timers,
unsafe { &qdev_prop_uint8 }, unsafe { &qdev_prop_uint8 },
@ -916,7 +915,7 @@ qemu_api::declare_properties! {
default = HPET_MIN_TIMERS default = HPET_MIN_TIMERS
), ),
qemu_api::define_property!( qemu_api::define_property!(
c_str!("msi"), c"msi",
HPETState, HPETState,
flags, flags,
unsafe { &qdev_prop_bit }, unsafe { &qdev_prop_bit },
@ -925,7 +924,7 @@ qemu_api::declare_properties! {
default = false, default = false,
), ),
qemu_api::define_property!( qemu_api::define_property!(
c_str!("hpet-intcap"), c"hpet-intcap",
HPETState, HPETState,
int_route_cap, int_route_cap,
unsafe { &qdev_prop_uint32 }, unsafe { &qdev_prop_uint32 },
@ -933,7 +932,7 @@ qemu_api::declare_properties! {
default = 0 default = 0
), ),
qemu_api::define_property!( qemu_api::define_property!(
c_str!("hpet-offset-saved"), c"hpet-offset-saved",
HPETState, HPETState,
hpet_offset_saved, hpet_offset_saved,
unsafe { &qdev_prop_bool }, unsafe { &qdev_prop_bool },
@ -974,7 +973,7 @@ unsafe extern "C" fn hpet_post_load(opaque: *mut c_void, version_id: c_int) -> c
} }
static VMSTATE_HPET_RTC_IRQ_LEVEL: VMStateDescription = VMStateDescription { 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, version_id: 1,
minimum_version_id: 1, minimum_version_id: 1,
needed: Some(hpet_rtc_irq_level_needed), needed: Some(hpet_rtc_irq_level_needed),
@ -985,7 +984,7 @@ static VMSTATE_HPET_RTC_IRQ_LEVEL: VMStateDescription = VMStateDescription {
}; };
static VMSTATE_HPET_OFFSET: VMStateDescription = VMStateDescription { static VMSTATE_HPET_OFFSET: VMStateDescription = VMStateDescription {
name: c_str!("hpet/offset").as_ptr(), name: c"hpet/offset".as_ptr(),
version_id: 1, version_id: 1,
minimum_version_id: 1, minimum_version_id: 1,
needed: Some(hpet_offset_needed), needed: Some(hpet_offset_needed),
@ -996,7 +995,7 @@ static VMSTATE_HPET_OFFSET: VMStateDescription = VMStateDescription {
}; };
static VMSTATE_HPET_TIMER: VMStateDescription = VMStateDescription { static VMSTATE_HPET_TIMER: VMStateDescription = VMStateDescription {
name: c_str!("hpet_timer").as_ptr(), name: c"hpet_timer".as_ptr(),
version_id: 1, version_id: 1,
minimum_version_id: 1, minimum_version_id: 1,
fields: vmstate_fields! { fields: vmstate_fields! {
@ -1011,10 +1010,10 @@ static VMSTATE_HPET_TIMER: VMStateDescription = VMStateDescription {
..Zeroable::ZERO ..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 { static VMSTATE_HPET: VMStateDescription = VMStateDescription {
name: c_str!("hpet").as_ptr(), name: c"hpet".as_ptr(),
version_id: 2, version_id: 2,
minimum_version_id: 1, minimum_version_id: 1,
pre_save: Some(hpet_pre_save), pre_save: Some(hpet_pre_save),

View file

@ -7,9 +7,7 @@
//! This library implements a device model for the IA-PC HPET (High //! This library implements a device model for the IA-PC HPET (High
//! Precision Event Timers) device in QEMU. //! Precision Event Timers) device in QEMU.
use qemu_api::c_str;
pub mod fw_cfg; pub mod fw_cfg;
pub mod hpet; pub mod hpet;
pub const TYPE_HPET: &::std::ffi::CStr = c_str!("hpet"); pub const TYPE_HPET: &::std::ffi::CStr = c"hpet";

View file

@ -20,7 +20,6 @@ _qemu_api_rs = static_library(
'src/callbacks.rs', 'src/callbacks.rs',
'src/cell.rs', 'src/cell.rs',
'src/chardev.rs', 'src/chardev.rs',
'src/c_str.rs',
'src/errno.rs', 'src/errno.rs',
'src/irq.rs', 'src/irq.rs',
'src/memory.rs', 'src/memory.rs',

View file

@ -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);
}
}

View file

@ -77,13 +77,13 @@
//! //!
//! ``` //! ```
//! # use qemu_api::prelude::*; //! # 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}; //! # use qemu_api::{sysbus::SysBusDevice, qom::Owned, qom::ParentField};
//! # const N_GPIOS: usize = 8; //! # const N_GPIOS: usize = 8;
//! # struct PL061Registers { /* ... */ } //! # struct PL061Registers { /* ... */ }
//! # unsafe impl ObjectType for PL061State { //! # unsafe impl ObjectType for PL061State {
//! # type Class = <SysBusDevice as ObjectType>::Class; //! # 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 { //! struct PL061State {
//! parent_obj: ParentField<SysBusDevice>, //! parent_obj: ParentField<SysBusDevice>,

View file

@ -15,7 +15,6 @@ pub mod prelude;
pub mod assertions; pub mod assertions;
pub mod bitops; pub mod bitops;
pub mod c_str;
pub mod callbacks; pub mod callbacks;
pub mod cell; pub mod cell;
pub mod chardev; pub mod chardev;

View file

@ -427,7 +427,7 @@ unsafe impl<T: VMState, const N: usize> VMState for [T; N] {
macro_rules! vmstate_unused { macro_rules! vmstate_unused {
($size:expr) => {{ ($size:expr) => {{
$crate::bindings::VMStateField { $crate::bindings::VMStateField {
name: $crate::c_str!("unused").as_ptr(), name: c"unused".as_ptr(),
size: $size, size: $size,
info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) }, info: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_info_unused_buffer) },
flags: $crate::bindings::VMStateFlags::VMS_BUFFER, flags: $crate::bindings::VMStateFlags::VMS_BUFFER,

View file

@ -6,7 +6,6 @@ use std::{ffi::CStr, ptr::addr_of};
use qemu_api::{ use qemu_api::{
bindings::{module_call_init, module_init_type, qdev_prop_bool}, bindings::{module_call_init, module_init_type, qdev_prop_bool},
c_str,
cell::{self, BqlCell}, cell::{self, BqlCell},
declare_properties, define_property, declare_properties, define_property,
prelude::*, prelude::*,
@ -21,7 +20,7 @@ mod vmstate_tests;
// Test that macros can compile. // Test that macros can compile.
pub static VMSTATE: VMStateDescription = VMStateDescription { pub static VMSTATE: VMStateDescription = VMStateDescription {
name: c_str!("name").as_ptr(), name: c"name".as_ptr(),
unmigratable: true, unmigratable: true,
..Zeroable::ZERO ..Zeroable::ZERO
}; };
@ -48,7 +47,7 @@ impl DummyClass {
declare_properties! { declare_properties! {
DUMMY_PROPERTIES, DUMMY_PROPERTIES,
define_property!( define_property!(
c_str!("migrate-clk"), c"migrate-clk",
DummyState, DummyState,
migrate_clock, migrate_clock,
unsafe { &qdev_prop_bool }, unsafe { &qdev_prop_bool },
@ -58,7 +57,7 @@ declare_properties! {
unsafe impl ObjectType for DummyState { unsafe impl ObjectType for DummyState {
type Class = DummyClass; type Class = DummyClass;
const TYPE_NAME: &'static CStr = c_str!("dummy"); const TYPE_NAME: &'static CStr = c"dummy";
} }
impl ObjectImpl for DummyState { impl ObjectImpl for DummyState {
@ -92,7 +91,7 @@ pub struct DummyChildClass {
unsafe impl ObjectType for DummyChildState { unsafe impl ObjectType for DummyChildState {
type Class = DummyChildClass; type Class = DummyChildClass;
const TYPE_NAME: &'static CStr = c_str!("dummy_child"); const TYPE_NAME: &'static CStr = c"dummy_child";
} }
impl ObjectImpl for DummyChildState { impl ObjectImpl for DummyChildState {

View file

@ -14,7 +14,6 @@ use qemu_api::{
vmstate_info_bool, vmstate_info_int32, vmstate_info_int64, vmstate_info_int8, vmstate_info_bool, vmstate_info_int32, vmstate_info_int64, vmstate_info_int8,
vmstate_info_uint64, vmstate_info_uint8, vmstate_info_unused_buffer, VMStateFlags, vmstate_info_uint64, vmstate_info_uint8, vmstate_info_unused_buffer, VMStateFlags,
}, },
c_str,
cell::{BqlCell, Opaque}, cell::{BqlCell, Opaque},
impl_vmstate_forward, impl_vmstate_forward,
vmstate::{VMStateDescription, VMStateField}, vmstate::{VMStateDescription, VMStateField},
@ -43,7 +42,7 @@ struct FooA {
} }
static VMSTATE_FOOA: VMStateDescription = VMStateDescription { static VMSTATE_FOOA: VMStateDescription = VMStateDescription {
name: c_str!("foo_a").as_ptr(), name: c"foo_a".as_ptr(),
version_id: 1, version_id: 1,
minimum_version_id: 1, minimum_version_id: 1,
fields: vmstate_fields! { fields: vmstate_fields! {
@ -173,7 +172,7 @@ fn validate_foob(_state: &FooB, _version_id: u8) -> bool {
} }
static VMSTATE_FOOB: VMStateDescription = VMStateDescription { static VMSTATE_FOOB: VMStateDescription = VMStateDescription {
name: c_str!("foo_b").as_ptr(), name: c"foo_b".as_ptr(),
version_id: 2, version_id: 2,
minimum_version_id: 1, minimum_version_id: 1,
fields: vmstate_fields! { fields: vmstate_fields! {
@ -337,7 +336,7 @@ struct FooC {
} }
static VMSTATE_FOOC: VMStateDescription = VMStateDescription { static VMSTATE_FOOC: VMStateDescription = VMStateDescription {
name: c_str!("foo_c").as_ptr(), name: c"foo_c".as_ptr(),
version_id: 3, version_id: 3,
minimum_version_id: 1, minimum_version_id: 1,
fields: vmstate_fields! { fields: vmstate_fields! {
@ -452,13 +451,13 @@ fn validate_food_2(_state: &FooD, _version_id: u8) -> bool {
} }
static VMSTATE_FOOD: VMStateDescription = VMStateDescription { static VMSTATE_FOOD: VMStateDescription = VMStateDescription {
name: c_str!("foo_d").as_ptr(), name: c"foo_d".as_ptr(),
version_id: 3, version_id: 3,
minimum_version_id: 1, minimum_version_id: 1,
fields: vmstate_fields! { fields: vmstate_fields! {
vmstate_validate!(FooD, c_str!("foo_d_0"), FooD::validate_food_0), vmstate_validate!(FooD, c"foo_d_0", FooD::validate_food_0),
vmstate_validate!(FooD, c_str!("foo_d_1"), FooD::validate_food_1), vmstate_validate!(FooD, c"foo_d_1", FooD::validate_food_1),
vmstate_validate!(FooD, c_str!("foo_d_2"), validate_food_2), vmstate_validate!(FooD, c"foo_d_2", validate_food_2),
}, },
..Zeroable::ZERO ..Zeroable::ZERO
}; };