From nobody Sun Sep 28 17:07:43 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1757328810; cv=none; d=zohomail.com; s=zohoarc; b=Tg4MesiqFHWHvrvA2BcxzzF41BUdwHurl7A+aUOSxnfsvRKusGvSBs78mOfLHrpHPCagUMzRgbuJxta1whtbbg89X/ryEsYZsMOwrFHBGhBpkC0WTgKIyB587EWgAyXafXviJOeYjLP20WiW2+OjS8R95120tZkx4/WAPN1gOKk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757328810; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=jVfhBtBlOjXzGqnT8KeTSsWVyRaSIgsWAZBRhULMJZ8=; b=gSDETV/hIzes096PNncwB6Ff48nolAi4vD8VAVTTc7aGfJS4QNt9TdoZFMta7OFzMAlHOXoKsZvXYlS22dvUaqhCulOScK7oF+0WT4ASC05hI6m8K85KpDtUa+KRHRSgoLneqjyMIS+oyMLSdfgACeqhjqrlAY9oC5HcEAtGnT8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757328810289642.0748166176709; Mon, 8 Sep 2025 03:53:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uvZSg-0006BS-4M; Mon, 08 Sep 2025 06:51:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvZSc-00068f-T1 for qemu-devel@nongnu.org; Mon, 08 Sep 2025 06:50:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uvZSK-000617-Hr for qemu-devel@nongnu.org; Mon, 08 Sep 2025 06:50:57 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-U1xS0S9HOwewj2o8F69Xaw-1; Mon, 08 Sep 2025 06:50:31 -0400 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3dacc10dd30so3153286f8f.0 for ; Mon, 08 Sep 2025 03:50:31 -0700 (PDT) Received: from [192.168.10.48] ([151.95.56.250]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3d9f3c36a78sm25571165f8f.48.2025.09.08.03.50.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Sep 2025 03:50:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757328633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jVfhBtBlOjXzGqnT8KeTSsWVyRaSIgsWAZBRhULMJZ8=; b=TVgSHCoBXrB14TB4H6CRPCQ/3Oi7vLlAGtRk2HbRawKhl3DontwhGz+TsIL29tIu7Aj5ug jZKaBYt2M3Udf74+Q0eNEjHX8fNiKkiFwXZXM/sFrhjPF9z5Liv74cAl1R/mCIuA26QMlH OgenOsEdqbZ2pjLCRYNANOgXvgZqLaE= X-MC-Unique: U1xS0S9HOwewj2o8F69Xaw-1 X-Mimecast-MFC-AGG-ID: U1xS0S9HOwewj2o8F69Xaw_1757328630 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757328629; x=1757933429; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jVfhBtBlOjXzGqnT8KeTSsWVyRaSIgsWAZBRhULMJZ8=; b=dTkcLy3aw/MclZTkcCuy+4qYNorxevMdxWtsOoqi5QWIvsGTmgi5YMDcCoFQ8dT7UV Fk75B3w2FuXqgAR0CANvFa8xy648fMgdhvuHqT4ylb3E6Vf905ggbSBTYn0oxo+mrzRc R2DBDNY+DxCzRMJAgObTEFnszWEcKzBhYU0unLQiS3RGT+p37rUNJQvExlBT5DfXeOKK x+WEWs1I2Ll1uAKLdPFvr6hAdLtvwj9a35dYWnkUNGhC7sAWwxmmESR6rry/yW9Cd5NW EladQxrihfRq3tMqHBQZ1m7SposL71+xPmpKwkENkso7ejMzZGk2jH9GfA0/2SYjF6A1 yQug== X-Gm-Message-State: AOJu0YzAHbYeRwUEBcKO9sU/yXWGQcJNoB9qwFoVwOpVbXeEry2x+bbl q9NlhhzNZZ98Eqe1Aqe53nqIPdPX+bBQy/W+BsVWh0zU/ZLdDWSAra2pfDnjXBioq5A889ohlLv vqjTN+iOf3enOVJ5RYAXS3zc4cdJ7n6OtXSt8V5CC1bKJ8VJy06LI4Acfo5st75mCV/74X/gUD3 eVCT+Sw89N98aksBB8yA+yhBDgIatXpUj/BZ9bN2lX X-Gm-Gg: ASbGnct1AR388g8zEV0M+YFYAZI04VKf27kI0nilCcSCn7f6FIuPHVkhxJUEpzQg0vZ fRiQKKW3sGpmpdvJPj27ai87FnkU+v9hrdugsP0JA4O6h6tHmnhvT+DJV1NiJI30cUwxmV9DToD hpODEJDzPHmuAMHwmXu4uByYtdbP8/kvPAKlegQnzLD0lrx33SSNx31N5VND6FguTC6+CwmoCaS UR08azz7h6MkYhAFJMSjKhJiLinoBIKva+6PDlxvrdPMz4t1aPo5rXVOK77s0amVWAOv4MnV57d 2HB1HVsC12QAQSo2p1HKIGiN8t0BL3rMLZ9fVb99lN2eigSXsGygKHZeLHfEKavC60gwRHoljTP /9anQ+JfU9oTGq0sPds3pLXkmAhJQ9nqxi4TB2HKaNSE= X-Received: by 2002:a05:6000:1449:b0:3e3:b6ef:7243 with SMTP id ffacd0b85a97d-3e643556a10mr5439473f8f.51.1757328629365; Mon, 08 Sep 2025 03:50:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKYtKVTX0xqubLBgFUa3J3cpAqRWlPUxrrsq24rKr6QAI4aZSIV2Gp+f0Fjrk4rie+lx8DPQ== X-Received: by 2002:a05:6000:1449:b0:3e3:b6ef:7243 with SMTP id ffacd0b85a97d-3e643556a10mr5439444f8f.51.1757328628699; Mon, 08 Sep 2025 03:50:28 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 09/33] rust: qdev: const_refs_to_static Date: Mon, 8 Sep 2025 12:49:41 +0200 Message-ID: <20250908105005.2119297-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250908105005.2119297-1-pbonzini@redhat.com> References: <20250908105005.2119297-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1757328812576116600 Content-Type: text/plain; charset="utf-8" Now that const_refs_static can be assumed, convert the members of the DeviceImpl trait from functions to constants. This lets the compiler know that they have a 'static lifetime, and removes the need for the weird "Box::leak()". Signed-off-by: Paolo Bonzini Reviewed-by: Zhao Liu --- rust/hw/char/pl011/src/device.rs | 4 +-- rust/hw/timer/hpet/src/device.rs | 9 ++----- rust/qemu-api-macros/src/lib.rs | 8 +++--- rust/qemu-api-macros/src/tests.rs | 44 ++++++++++++++----------------- rust/qemu-api/src/qdev.rs | 19 +++++-------- rust/qemu-api/tests/tests.rs | 4 +-- 6 files changed, 33 insertions(+), 55 deletions(-) diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/devi= ce.rs index 87a17716fed..8411db8d00c 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -172,9 +172,7 @@ impl ObjectImpl for PL011State { } =20 impl DeviceImpl for PL011State { - fn vmsd() -> Option> { - Some(VMSTATE_PL011) - } + const VMSTATE: Option> =3D Some(VMSTATE_PL011= ); const REALIZE: Option qemu_api::Result<()>> =3D Some(Self= ::realize); } =20 diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/devi= ce.rs index eb5bd042b1c..dd5326a40d4 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -1008,16 +1008,11 @@ impl ObjectImpl for HPETState { // SAFETY: HPET_PROPERTIES is a valid Property array constructed with the // qemu_api::declare_properties macro. unsafe impl qemu_api::qdev::DevicePropertiesImpl for HPETState { - fn properties() -> &'static [Property] { - &HPET_PROPERTIES - } + const PROPERTIES: &'static [Property] =3D &HPET_PROPERTIES; } =20 impl DeviceImpl for HPETState { - fn vmsd() -> Option> { - Some(VMSTATE_HPET) - } - + const VMSTATE: Option> =3D Some(VMSTATE_HPET); const REALIZE: Option qemu_api::Result<()>> =3D Some(Self= ::realize); } =20 diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib= .rs index 97b2c214b62..a65a7ce2fe9 100644 --- a/rust/qemu-api-macros/src/lib.rs +++ b/rust/qemu-api-macros/src/lib.rs @@ -289,11 +289,9 @@ macro_rules! str_to_c_str { =20 Ok(quote_spanned! {input.span() =3D> unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for #name { - fn properties() -> &'static [::qemu_api::bindings::Property] { - static PROPERTIES: &[::qemu_api::bindings::Property] =3D &= [#(#properties_expanded),*]; - - PROPERTIES - } + const PROPERTIES: &'static [::qemu_api::bindings::Property] = =3D &[ + #(#properties_expanded),* + ]; } }) } diff --git a/rust/qemu-api-macros/src/tests.rs b/rust/qemu-api-macros/src/t= ests.rs index aafffcdce91..0e5a5728908 100644 --- a/rust/qemu-api-macros/src/tests.rs +++ b/rust/qemu-api-macros/src/tests.rs @@ -101,18 +101,16 @@ pub struct DummyState { }, quote! { unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for DummySt= ate { - fn properties() -> &'static [::qemu_api::bindings::Propert= y] { - static PROPERTIES: &[::qemu_api::bindings::Property] = =3D - &[::qemu_api::bindings::Property { - name: ::std::ffi::CStr::as_ptr(c"migrate_clock= "), - info: ::VA= LUE, - offset: ::core::mem::offset_of!(DummyState, mi= grate_clock) as isize, - set_default: true, - defval: ::qemu_api::bindings::Property__bindge= n_ty_1 { u: true as u64 }, - ..::qemu_api::zeroable::Zeroable::ZERO - }]; - PROPERTIES - } + const PROPERTIES: &'static [::qemu_api::bindings::Property= ] =3D &[ + ::qemu_api::bindings::Property { + name: ::std::ffi::CStr::as_ptr(c"migrate_clock"), + info: ::VALUE, + offset: ::core::mem::offset_of!(DummyState, migrat= e_clock) as isize, + set_default: true, + defval: ::qemu_api::bindings::Property__bindgen_ty= _1 { u: true as u64 }, + ..::qemu_api::zeroable::Zeroable::ZERO + } + ]; } } ); @@ -130,18 +128,16 @@ pub struct DummyState { }, quote! { unsafe impl ::qemu_api::qdev::DevicePropertiesImpl for DummySt= ate { - fn properties() -> &'static [::qemu_api::bindings::Propert= y] { - static PROPERTIES: &[::qemu_api::bindings::Property] = =3D - &[::qemu_api::bindings::Property { - name: ::std::ffi::CStr::as_ptr(c"migrate-clk"), - info: ::VA= LUE, - offset: ::core::mem::offset_of!(DummyState, mi= grate_clock) as isize, - set_default: true, - defval: ::qemu_api::bindings::Property__bindge= n_ty_1 { u: true as u64 }, - ..::qemu_api::zeroable::Zeroable::ZERO - }]; - PROPERTIES - } + const PROPERTIES: &'static [::qemu_api::bindings::Property= ] =3D &[ + ::qemu_api::bindings::Property { + name: ::std::ffi::CStr::as_ptr(c"migrate-clk"), + info: ::VALUE, + offset: ::core::mem::offset_of!(DummyState, migrat= e_clock) as isize, + set_default: true, + defval: ::qemu_api::bindings::Property__bindgen_ty= _1 { u: true as u64 }, + ..::qemu_api::zeroable::Zeroable::ZERO + } + ]; } } ); diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs index 4dda8c81131..436142d8aef 100644 --- a/rust/qemu-api/src/qdev.rs +++ b/rust/qemu-api/src/qdev.rs @@ -151,11 +151,8 @@ unsafe impl QDevProp for crate::chardev::CharBackend { /// Caller is responsible for the validity of properties array. pub unsafe trait DevicePropertiesImpl { /// An array providing the properties that the user can set on the - /// device. Not a `const` because referencing statics in constants - /// is unstable until Rust 1.83.0. - fn properties() -> &'static [Property] { - &[] - } + /// device. + const PROPERTIES: &'static [Property] =3D &[]; } =20 /// Trait providing the contents of [`DeviceClass`]. @@ -173,9 +170,7 @@ pub trait DeviceImpl: /// A `VMStateDescription` providing the migration format for the devi= ce /// Not a `const` because referencing statics in constants is unstable /// until Rust 1.83.0. - fn vmsd() -> Option> { - None - } + const VMSTATE: Option> =3D None; } =20 /// # Safety @@ -224,12 +219,10 @@ pub fn class_init(&mut self) { if ::REALIZE.is_some() { self.realize =3D Some(rust_realize_fn::); } - if let Some(vmsd) =3D ::vmsd() { - // Give a 'static lifetime to the return value of vmsd(). - // Temporary until vmsd() can be changed into a const. - self.vmsd =3D Box::leak(Box::new(vmsd.get())); + if let Some(ref vmsd) =3D ::VMSTATE { + self.vmsd =3D vmsd.as_ref(); } - let prop =3D ::properties(); + let prop =3D ::PROPERTIES; if !prop.is_empty() { unsafe { bindings::device_class_set_props_n(self, prop.as_ptr(), pr= op.len()); diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs index 4d4e4653f38..13495687419 100644 --- a/rust/qemu-api/tests/tests.rs +++ b/rust/qemu-api/tests/tests.rs @@ -56,9 +56,7 @@ impl ObjectImpl for DummyState { impl ResettablePhasesImpl for DummyState {} =20 impl DeviceImpl for DummyState { - fn vmsd() -> Option> { - Some(VMSTATE) - } + const VMSTATE: Option> =3D Some(VMSTATE); } =20 #[repr(C)] --=20 2.51.0