rust: qdev: move device_class_init! body to generic function, ClassInitImpl implementation to macro

Use a trait to access the former parameters to device_class_init!.
This allows hiding the details of the class_init implementation behind
a generic function and makes higher-level functionality available from
qemu_api.

The implementation of ClassInitImpl is then the same for all devices and
is easily macroized.  Later on, we can remove the need to implement
ClassInitImpl by hand for all device types, and stop making
rust_device_class_init<>() public.

While at it, document the members of DeviceImpl.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2024-10-28 10:29:27 +01:00
parent c6c4f3e0d9
commit 8c80c472da
4 changed files with 103 additions and 56 deletions

View file

@ -12,11 +12,13 @@ use qemu_api::{
bindings::{self, *},
c_str,
definitions::ObjectImpl,
device_class::TYPE_SYS_BUS_DEVICE,
device_class::{DeviceImpl, TYPE_SYS_BUS_DEVICE},
impl_device_class,
irq::InterruptSource,
};
use crate::{
device_class,
memory_ops::PL011_OPS,
registers::{self, Interrupt},
RegisterOffset,
@ -116,14 +118,20 @@ pub struct PL011Class {
_inner: [u8; 0],
}
impl qemu_api::definitions::ClassInitImpl for PL011State {
const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
Some(crate::device_class::pl011_class_init);
const CLASS_BASE_INIT: Option<
unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
> = None;
impl DeviceImpl for PL011State {
fn properties() -> &'static [Property] {
&device_class::PL011_PROPERTIES
}
fn vmsd() -> Option<&'static VMStateDescription> {
Some(&device_class::VMSTATE_PL011)
}
const REALIZE: Option<unsafe extern "C" fn(*mut DeviceState, *mut *mut Error)> =
Some(device_class::pl011_realize);
const RESET: Option<unsafe extern "C" fn(*mut DeviceState)> = Some(device_class::pl011_reset);
}
impl_device_class!(PL011State);
impl PL011State {
/// Initializes a pre-allocated, unitialized instance of `PL011State`.
///
@ -649,17 +657,13 @@ pub unsafe extern "C" fn pl011_luminary_init(obj: *mut Object) {
}
}
impl qemu_api::definitions::ClassInitImpl for PL011Luminary {
const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
None;
const CLASS_BASE_INIT: Option<
unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
> = None;
}
impl ObjectImpl for PL011Luminary {
type Class = PL011LuminaryClass;
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);
}
impl DeviceImpl for PL011Luminary {}
impl_device_class!(PL011Luminary);