diff mbox series

[16/26] rust: qom: change the parent type to an associated type

Message ID 20241209123717.99077-17-pbonzini@redhat.com (mailing list archive)
State New
Headers show
Series rust: bundle of prerequisites for HPET implementation | expand

Commit Message

Paolo Bonzini Dec. 9, 2024, 12:37 p.m. UTC
Avoid duplicated code to retrieve the QOM type strings from the
Rust type.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/hw/char/pl011/src/device.rs | 6 ++++--
 rust/qemu-api/src/definitions.rs | 8 ++------
 rust/qemu-api/tests/tests.rs     | 3 +--
 3 files changed, 7 insertions(+), 10 deletions(-)

Comments

Zhao Liu Dec. 11, 2024, 8:47 a.m. UTC | #1
On Mon, Dec 09, 2024 at 01:37:07PM +0100, Paolo Bonzini wrote:
> Date: Mon,  9 Dec 2024 13:37:07 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 16/26] rust: qom: change the parent type to an associated
>  type
> X-Mailer: git-send-email 2.47.1
> 
> Avoid duplicated code to retrieve the QOM type strings from the
> Rust type.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  rust/hw/char/pl011/src/device.rs | 6 ++++--
>  rust/qemu-api/src/definitions.rs | 8 ++------
>  rust/qemu-api/tests/tests.rs     | 3 +--
>  3 files changed, 7 insertions(+), 10 deletions(-)
> 

...
> @@ -76,11 +76,7 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl {
>  
>      const TYPE_INFO: TypeInfo = TypeInfo {
>          name: Self::TYPE_NAME.as_ptr(),
> -        parent: if let Some(pname) = Self::PARENT_TYPE_NAME {
> -            pname.as_ptr()
> -        } else {
> -            core::ptr::null_mut()
> -        },
> +        parent: Self::ParentType::TYPE_NAME.as_ptr(),

Object is implemented at C side, so at leaset the ParentType should be
TYPE_OBJECT.

>          instance_size: core::mem::size_of::<Self>(),
>          instance_align: core::mem::align_of::<Self>(),
>          instance_init: match Self::INSTANCE_INIT {

Great!

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

Patch

diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index 0ab825b1ca4..3e29442a625 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -113,7 +113,8 @@  unsafe impl ObjectType for PL011State {
 }
 
 impl ObjectImpl for PL011State {
-    const PARENT_TYPE_NAME: Option<&'static CStr> = Some(<SysBusDevice as ObjectType>::TYPE_NAME);
+    type ParentType = SysBusDevice;
+
     const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
 }
 
@@ -650,7 +651,8 @@  unsafe impl ObjectType for PL011Luminary {
 }
 
 impl ObjectImpl for PL011Luminary {
-    const PARENT_TYPE_NAME: Option<&'static CStr> = Some(<PL011State as ObjectType>::TYPE_NAME);
+    type ParentType = PL011State;
+
     const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
 }
 
diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitions.rs
index 1c412dbc876..1975ad91867 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -57,7 +57,7 @@  pub unsafe trait ObjectType: Sized {
 pub trait ObjectImpl: ObjectType + ClassInitImpl {
     /// The parent of the type.  This should match the first field of
     /// the struct that implements `ObjectImpl`:
-    const PARENT_TYPE_NAME: Option<&'static CStr>;
+    type ParentType: ObjectType;
 
     /// Whether the object can be instantiated
     const ABSTRACT: bool = false;
@@ -76,11 +76,7 @@  pub trait ObjectImpl: ObjectType + ClassInitImpl {
 
     const TYPE_INFO: TypeInfo = TypeInfo {
         name: Self::TYPE_NAME.as_ptr(),
-        parent: if let Some(pname) = Self::PARENT_TYPE_NAME {
-            pname.as_ptr()
-        } else {
-            core::ptr::null_mut()
-        },
+        parent: Self::ParentType::TYPE_NAME.as_ptr(),
         instance_size: core::mem::size_of::<Self>(),
         instance_align: core::mem::align_of::<Self>(),
         instance_init: match Self::INSTANCE_INIT {
diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs
index 1be03eb685c..2a72b0f9276 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -53,8 +53,7 @@  unsafe impl ObjectType for DummyState {
     }
 
     impl ObjectImpl for DummyState {
-        const PARENT_TYPE_NAME: Option<&'static CStr> =
-            Some(<DeviceState as ObjectType>::TYPE_NAME);
+        type ParentType = DeviceState;
         const ABSTRACT: bool = false;
     }