On 12/12/24 10:40, Zhao Liu wrote:
On Mon, Dec 09, 2024 at 01:37:09PM +0100, Paolo Bonzini wrote:
Date: Mon,  9 Dec 2024 13:37:09 +0100
From: Paolo Bonzini <pbonz...@redhat.com>
Subject: [PATCH 18/26] rust: qom: add possibility of overriding unparent
X-Mailer: git-send-email 2.47.1

Add a blanket definition of ClassInitImpl<ObjectClass> that thunks
ObjectImpl::UNPARENT and overrides it in ObjectClass if it is not
None.

ClassInitImpl<DeviceClass> can now call its superclass's ClassInitImpl,
so that the C and Rust hierarchies match more closely.

This is mostly done as an example of implementing the metaclass
hierarchy under ClassInitImpl.

Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
---
  rust/qemu-api/src/definitions.rs  | 44 ++++++++++++++++++++++++++++---
  rust/qemu-api/src/device_class.rs |  6 +++--
  2 files changed, 45 insertions(+), 5 deletions(-)

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

(with an additional comment below...)

diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitions.rs
index 2429b9f53f0..958ea34decc 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -6,7 +6,7 @@
use std::{ffi::CStr, os::raw::c_void}; -use crate::bindings::{Object, ObjectClass, TypeInfo};
+use crate::bindings::{self, Object, ObjectClass, TypeInfo};
unsafe extern "C" fn rust_instance_init<T: ObjectImpl>(obj: *mut Object) {
      // SAFETY: obj is an instance of T, since rust_instance_init<T>
@@ -115,6 +115,9 @@ pub trait ObjectImpl: ObjectType + 
ClassInitImpl<Self::Class> {
          class_data: core::ptr::null_mut(),
          interfaces: core::ptr::null_mut(),
      };
+
+    // methods on ObjectClass
+    const UNPARENT: Option<fn(&mut Self)> = None;
  }

Will we change `&mut Self` to an immutable reference in the future?

Good point, let's do it now since anyway UNPARENT is unused.

Paolo


Reply via email to