[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 43/49] rust: qom: convert type_info! macro to an associated const
From: |
Paolo Bonzini |
Subject: |
[PULL 43/49] rust: qom: convert type_info! macro to an associated const |
Date: |
Wed, 11 Dec 2024 17:27:13 +0100 |
type_info! is only used in the definition of ObjectImpl::TYPE_INFO, and
in fact in all of them. Pull type_info!'s definition into the ObjectImpl
trait, thus simplifying the external interface of qemu_api::definitions.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 6 ++--
rust/qemu-api/src/definitions.rs | 50 ++++++++++++++------------------
rust/qemu-api/tests/tests.rs | 1 -
3 files changed, 24 insertions(+), 33 deletions(-)
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index bd12067aaf0..bcb146c24d6 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -106,7 +106,6 @@ pub struct PL011State {
impl ObjectImpl for PL011State {
type Class = PL011Class;
- const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! {
Self };
const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE);
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> =
Some(pl011_init);
@@ -149,7 +148,7 @@ unsafe fn init(&mut self) {
addr_of_mut!(*self).cast::<Object>(),
&PL011_OPS,
addr_of_mut!(*self).cast::<c_void>(),
- Self::TYPE_INFO.name,
+ Self::TYPE_NAME.as_ptr(),
0x1000,
);
sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
@@ -598,7 +597,7 @@ pub fn post_load(&mut self, _version_id: u32) -> Result<(),
()> {
chr: *mut Chardev,
) -> *mut DeviceState {
unsafe {
- let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name);
+ let dev: *mut DeviceState = qdev_new(PL011State::TYPE_NAME.as_ptr());
let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
qdev_prop_set_chr(dev, c_str!("chardev").as_ptr(), chr);
@@ -660,7 +659,6 @@ impl qemu_api::definitions::ClassInitImpl for
PL011LuminaryClass {
impl ObjectImpl for PL011Luminary {
type Class = PL011LuminaryClass;
- const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! {
Self };
const TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(crate::TYPE_PL011);
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> =
Some(pl011_luminary_init);
diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitions.rs
index 3291f4242ce..6ecfaf51b09 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -9,15 +9,34 @@
use crate::bindings::{Object, ObjectClass, TypeInfo};
/// Trait a type must implement to be registered with QEMU.
-pub trait ObjectImpl {
- type Class;
- const TYPE_INFO: TypeInfo;
+pub trait ObjectImpl: Sized {
+ type Class: ClassInitImpl;
const TYPE_NAME: &'static CStr;
const PARENT_TYPE_NAME: Option<&'static CStr>;
const ABSTRACT: bool = false;
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> =
None;
const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> =
None;
+
+ 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()
+ },
+ instance_size: core::mem::size_of::<Self>(),
+ instance_align: core::mem::align_of::<Self>(),
+ instance_init: Self::INSTANCE_INIT,
+ instance_post_init: Self::INSTANCE_POST_INIT,
+ instance_finalize: Self::INSTANCE_FINALIZE,
+ abstract_: Self::ABSTRACT,
+ class_size: core::mem::size_of::<Self::Class>(),
+ class_init: <Self::Class as ClassInitImpl>::CLASS_INIT,
+ class_base_init: <Self::Class as ClassInitImpl>::CLASS_BASE_INIT,
+ class_data: core::ptr::null_mut(),
+ interfaces: core::ptr::null_mut(),
+ };
}
/// Trait used to fill in a class struct.
@@ -83,28 +102,3 @@ extern "C" fn ctor_fn() {
}
};
}
-
-#[macro_export]
-macro_rules! type_info {
- ($t:ty) => {
- $crate::bindings::TypeInfo {
- name: <$t as $crate::definitions::ObjectImpl>::TYPE_NAME.as_ptr(),
- parent: if let Some(pname) = <$t as
$crate::definitions::ObjectImpl>::PARENT_TYPE_NAME {
- pname.as_ptr()
- } else {
- ::core::ptr::null_mut()
- },
- instance_size: ::core::mem::size_of::<$t>(),
- instance_align: ::core::mem::align_of::<$t>(),
- instance_init: <$t as
$crate::definitions::ObjectImpl>::INSTANCE_INIT,
- instance_post_init: <$t as
$crate::definitions::ObjectImpl>::INSTANCE_POST_INIT,
- instance_finalize: <$t as
$crate::definitions::ObjectImpl>::INSTANCE_FINALIZE,
- abstract_: <$t as $crate::definitions::ObjectImpl>::ABSTRACT,
- class_size: ::core::mem::size_of::<<$t as
$crate::definitions::ObjectImpl>::Class>(),
- class_init: <<$t as $crate::definitions::ObjectImpl>::Class as
$crate::definitions::ClassInitImpl>::CLASS_INIT,
- class_base_init: <<$t as $crate::definitions::ObjectImpl>::Class
as $crate::definitions::ClassInitImpl>::CLASS_BASE_INIT,
- class_data: ::core::ptr::null_mut(),
- interfaces: ::core::ptr::null_mut(),
- };
- }
-}
diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs
index 704c63c846f..7f9df348b00 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -55,7 +55,6 @@ pub struct DummyClass {
impl ObjectImpl for DummyState {
type Class = DummyClass;
- const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! {
Self };
const TYPE_NAME: &'static CStr = c_str!("dummy");
const PARENT_TYPE_NAME: Option<&'static CStr> =
Some(device_class::TYPE_DEVICE);
}
--
2.47.1
- [PULL 34/49] qom/object: Remove type_register(), (continued)
- [PULL 34/49] qom/object: Remove type_register(), Paolo Bonzini, 2024/12/11
- [PULL 35/49] bql: check that the BQL is not dropped within marked sections, Paolo Bonzini, 2024/12/11
- [PULL 36/49] rust: cell: add BQL-enforcing Cell variant, Paolo Bonzini, 2024/12/11
- [PULL 38/49] rust: define prelude, Paolo Bonzini, 2024/12/11
- [PULL 37/49] rust: cell: add BQL-enforcing RefCell variant, Paolo Bonzini, 2024/12/11
- [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 <=
- [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, 2024/12/11
- Re: [PULL 00/49] rust, QOM, kvm changes for 2024-12-11, Stefan Hajnoczi, 2024/12/12