[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 49/49] rust: qom: change the parent type to an associated type
From: |
Paolo Bonzini |
Subject: |
[PULL 49/49] rust: qom: change the parent type to an associated type |
Date: |
Wed, 11 Dec 2024 17:27:19 +0100 |
Avoid duplicated code to retrieve the QOM type strings from the
Rust type.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 6 ++++--
rust/qemu-api/src/definitions.rs | 12 ++++--------
rust/qemu-api/tests/tests.rs | 3 +--
3 files changed, 9 insertions(+), 12 deletions(-)
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 b98a6926785..df91a2e31a9 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -45,10 +45,10 @@
/// - the struct must be `#[repr(C)]`;
///
/// - the first field of the struct must be of the instance struct
corresponding
-/// to the superclass, as declared in `ObjectImpl::PARENT_TYPE_NAME`
+/// to the superclass, which is `ObjectImpl::ParentType`
///
/// - likewise, the first field of the `Class` must be of the class struct
-/// corresponding to the superclass
+/// corresponding to the superclass, which is
`ObjectImpl::ParentType::Class`.
pub unsafe trait ObjectType: Sized {
/// The QOM class object corresponding to this struct. Not used yet.
type Class;
@@ -62,7 +62,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;
@@ -82,11 +82,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 1d027dd6527..278efe967fe 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -48,8 +48,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;
}
--
2.47.1
- [PULL 39/49] rust: add bindings for interrupt sources, (continued)
- [PULL 39/49] rust: add bindings for interrupt sources, Paolo Bonzini, 2024/12/11
- [PULL 40/49] rust: add a bit operation module, Paolo Bonzini, 2024/12/11
- [PULL 41/49] rust: qom: add default definitions for ObjectImpl, Paolo Bonzini, 2024/12/11
- [PULL 42/49] rust: qom: rename Class trait to ClassInitImpl, Paolo Bonzini, 2024/12/11
- [PULL 43/49] rust: qom: convert type_info! macro to an associated const, Paolo Bonzini, 2024/12/11
- [PULL 44/49] rust: qom: move ClassInitImpl to the instance side, Paolo Bonzini, 2024/12/11
- [PULL 45/49] rust: qdev: move device_class_init! body to generic function, ClassInitImpl implementation to macro, Paolo Bonzini, 2024/12/11
- [PULL 46/49] rust: qdev: move bridge for realize and reset functions out of pl011, Paolo Bonzini, 2024/12/11
- [PULL 47/49] rust: qom: move bridge for TypeInfo functions out of pl011, Paolo Bonzini, 2024/12/11
- [PULL 48/49] rust: qom: split ObjectType from ObjectImpl trait, Paolo Bonzini, 2024/12/11
- [PULL 49/49] rust: qom: change the parent type to an associated type,
Paolo Bonzini <=
- Re: [PULL 00/49] rust, QOM, kvm changes for 2024-12-11, Stefan Hajnoczi, 2024/12/12