mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 04:13:53 -06:00
rust: qom: convert type_info! macro to an associated const
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>
This commit is contained in:
parent
93ea0896ea
commit
3701fb22df
3 changed files with 24 additions and 33 deletions
|
@ -106,7 +106,6 @@ pub struct PL011State {
|
||||||
|
|
||||||
impl ObjectImpl for PL011State {
|
impl ObjectImpl for PL011State {
|
||||||
type Class = PL011Class;
|
type Class = PL011Class;
|
||||||
const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
|
|
||||||
const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
|
const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE);
|
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);
|
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_init);
|
||||||
|
@ -149,7 +148,7 @@ impl PL011State {
|
||||||
addr_of_mut!(*self).cast::<Object>(),
|
addr_of_mut!(*self).cast::<Object>(),
|
||||||
&PL011_OPS,
|
&PL011_OPS,
|
||||||
addr_of_mut!(*self).cast::<c_void>(),
|
addr_of_mut!(*self).cast::<c_void>(),
|
||||||
Self::TYPE_INFO.name,
|
Self::TYPE_NAME.as_ptr(),
|
||||||
0x1000,
|
0x1000,
|
||||||
);
|
);
|
||||||
sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
|
sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
|
||||||
|
@ -598,7 +597,7 @@ pub unsafe extern "C" fn pl011_create(
|
||||||
chr: *mut Chardev,
|
chr: *mut Chardev,
|
||||||
) -> *mut DeviceState {
|
) -> *mut DeviceState {
|
||||||
unsafe {
|
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>();
|
let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
|
||||||
|
|
||||||
qdev_prop_set_chr(dev, c_str!("chardev").as_ptr(), chr);
|
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 {
|
impl ObjectImpl for PL011Luminary {
|
||||||
type Class = PL011LuminaryClass;
|
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 TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(crate::TYPE_PL011);
|
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);
|
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_luminary_init);
|
||||||
|
|
|
@ -9,15 +9,34 @@ use std::{ffi::CStr, os::raw::c_void};
|
||||||
use crate::bindings::{Object, ObjectClass, TypeInfo};
|
use crate::bindings::{Object, ObjectClass, TypeInfo};
|
||||||
|
|
||||||
/// Trait a type must implement to be registered with QEMU.
|
/// Trait a type must implement to be registered with QEMU.
|
||||||
pub trait ObjectImpl {
|
pub trait ObjectImpl: Sized {
|
||||||
type Class;
|
type Class: ClassInitImpl;
|
||||||
const TYPE_INFO: TypeInfo;
|
|
||||||
const TYPE_NAME: &'static CStr;
|
const TYPE_NAME: &'static CStr;
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr>;
|
const PARENT_TYPE_NAME: Option<&'static CStr>;
|
||||||
const ABSTRACT: bool = false;
|
const ABSTRACT: bool = false;
|
||||||
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
|
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_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
|
||||||
const INSTANCE_FINALIZE: 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.
|
/// Trait used to fill in a class struct.
|
||||||
|
@ -83,28 +102,3 @@ macro_rules! module_init {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -55,7 +55,6 @@ fn test_device_decl_macros() {
|
||||||
|
|
||||||
impl ObjectImpl for DummyState {
|
impl ObjectImpl for DummyState {
|
||||||
type Class = DummyClass;
|
type Class = DummyClass;
|
||||||
const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
|
|
||||||
const TYPE_NAME: &'static CStr = c_str!("dummy");
|
const TYPE_NAME: &'static CStr = c_str!("dummy");
|
||||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(device_class::TYPE_DEVICE);
|
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(device_class::TYPE_DEVICE);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue