rust: qom: put class_init together from multiple ClassInitImpl<>

Parameterize the implementation of ClassInitImpl so that it is
possible to call up the chain of implementations, one superclass at
a time starting at ClassInitImpl<Self::Class>.

In order to avoid having to implement (for example)
ClassInitImpl<PL011Class>, also remove the dummy PL011Class and
PL011LuminaryClass structs and specify the same ObjectType::Class as
the superclass.  In the future this default behavior can be handled by
a procedural macro, by looking at the first field in the struct.

Note that the new trait is safe: the calls are started by
rust_class_init<>(), which is not public and can convert the class
pointer to a Rust reference.

Since CLASS_BASE_INIT applies to the type that is being defined,
and only to it, move it to ObjectImpl.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2024-11-29 15:19:23 +01:00
parent b1987a2547
commit 6dd818fbbb
5 changed files with 127 additions and 80 deletions

View file

@ -6,7 +6,13 @@ use std::{ffi::CStr, ptr::addr_of};
pub use bindings::{SysBusDevice, SysBusDeviceClass};
use crate::{bindings, cell::bql_locked, irq::InterruptSource, prelude::*};
use crate::{
bindings::{self, DeviceClass},
cell::bql_locked,
definitions::ClassInitImpl,
irq::InterruptSource,
prelude::*,
};
unsafe impl ObjectType for SysBusDevice {
type Class = SysBusDeviceClass;
@ -14,6 +20,16 @@ unsafe impl ObjectType for SysBusDevice {
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
}
// TODO: add SysBusDeviceImpl
impl<T> ClassInitImpl<SysBusDeviceClass> for T
where
T: ClassInitImpl<DeviceClass>,
{
fn class_init(sdc: &mut SysBusDeviceClass) {
<T as ClassInitImpl<DeviceClass>>::class_init(&mut sdc.parent_class);
}
}
impl SysBusDevice {
/// Return `self` cast to a mutable pointer, for use in calls to C code.
const fn as_mut_ptr(&self) -> *mut SysBusDevice {