rust: qom: change the parent type to an associated type
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>
This commit is contained in:
parent
7bd8e3ef63
commit
166e8a1fd1
@ -113,7 +113,8 @@ unsafe impl ObjectType for PL011State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl 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);
|
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -650,7 +651,8 @@ unsafe impl ObjectType for PL011Luminary {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl 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);
|
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,10 +45,10 @@ unsafe extern "C" fn rust_instance_post_init<T: ObjectImpl>(obj: *mut Object) {
|
|||||||
/// - the struct must be `#[repr(C)]`;
|
/// - the struct must be `#[repr(C)]`;
|
||||||
///
|
///
|
||||||
/// - the first field of the struct must be of the instance struct corresponding
|
/// - 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
|
/// - 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 {
|
pub unsafe trait ObjectType: Sized {
|
||||||
/// The QOM class object corresponding to this struct. Not used yet.
|
/// The QOM class object corresponding to this struct. Not used yet.
|
||||||
type Class;
|
type Class;
|
||||||
@ -62,7 +62,7 @@ pub unsafe trait ObjectType: Sized {
|
|||||||
pub trait ObjectImpl: ObjectType + ClassInitImpl {
|
pub trait ObjectImpl: ObjectType + ClassInitImpl {
|
||||||
/// The parent of the type. This should match the first field of
|
/// The parent of the type. This should match the first field of
|
||||||
/// the struct that implements `ObjectImpl`:
|
/// the struct that implements `ObjectImpl`:
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr>;
|
type ParentType: ObjectType;
|
||||||
|
|
||||||
/// Whether the object can be instantiated
|
/// Whether the object can be instantiated
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
@ -82,11 +82,7 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl {
|
|||||||
|
|
||||||
const TYPE_INFO: TypeInfo = TypeInfo {
|
const TYPE_INFO: TypeInfo = TypeInfo {
|
||||||
name: Self::TYPE_NAME.as_ptr(),
|
name: Self::TYPE_NAME.as_ptr(),
|
||||||
parent: if let Some(pname) = Self::PARENT_TYPE_NAME {
|
parent: Self::ParentType::TYPE_NAME.as_ptr(),
|
||||||
pname.as_ptr()
|
|
||||||
} else {
|
|
||||||
core::ptr::null_mut()
|
|
||||||
},
|
|
||||||
instance_size: core::mem::size_of::<Self>(),
|
instance_size: core::mem::size_of::<Self>(),
|
||||||
instance_align: core::mem::align_of::<Self>(),
|
instance_align: core::mem::align_of::<Self>(),
|
||||||
instance_init: match Self::INSTANCE_INIT {
|
instance_init: match Self::INSTANCE_INIT {
|
||||||
|
@ -48,8 +48,7 @@ fn test_device_decl_macros() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl for DummyState {
|
impl ObjectImpl for DummyState {
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr> =
|
type ParentType = DeviceState;
|
||||||
Some(<DeviceState as ObjectType>::TYPE_NAME);
|
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user