mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-30 22:03:54 -06:00
rust: qom: split ObjectType from ObjectImpl trait
Define a separate trait for fields that also applies to classes that are defined by C code. This makes it possible to add metadata to core classes, which has multiple uses: - it makes it possible to access the parent struct's TYPE_* for types that are defined in Rust code, and to avoid repeating it in every subclass - implementors of ObjectType will be allowed to implement the IsA<> trait and therefore to perform typesafe casts from one class to another. - in the future, an ObjectType could be created with Foo::new() in a type-safe manner, without having to pass a TYPE_* constant. Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
1f9d52c938
commit
7bd8e3ef63
6 changed files with 58 additions and 26 deletions
|
@ -12,9 +12,10 @@ use qemu_api::{
|
||||||
bindings::{self, *},
|
bindings::{self, *},
|
||||||
c_str,
|
c_str,
|
||||||
definitions::ObjectImpl,
|
definitions::ObjectImpl,
|
||||||
device_class::{DeviceImpl, TYPE_SYS_BUS_DEVICE},
|
device_class::DeviceImpl,
|
||||||
impl_device_class,
|
impl_device_class,
|
||||||
irq::InterruptSource,
|
irq::InterruptSource,
|
||||||
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -106,10 +107,13 @@ pub struct PL011State {
|
||||||
device_id: DeviceId,
|
device_id: DeviceId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl for PL011State {
|
unsafe impl ObjectType for PL011State {
|
||||||
type Class = PL011Class;
|
type Class = PL011Class;
|
||||||
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);
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for PL011State {
|
||||||
|
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(<SysBusDevice as ObjectType>::TYPE_NAME);
|
||||||
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -640,10 +644,13 @@ impl PL011Luminary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl for PL011Luminary {
|
unsafe impl ObjectType for PL011Luminary {
|
||||||
type Class = PL011LuminaryClass;
|
type Class = PL011LuminaryClass;
|
||||||
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);
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for PL011Luminary {
|
||||||
|
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(<PL011State as ObjectType>::TYPE_NAME);
|
||||||
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,25 +26,40 @@ unsafe extern "C" fn rust_instance_post_init<T: ObjectImpl>(obj: *mut Object) {
|
||||||
T::INSTANCE_POST_INIT.unwrap()(unsafe { &mut *obj.cast::<T>() })
|
T::INSTANCE_POST_INIT.unwrap()(unsafe { &mut *obj.cast::<T>() })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait a type must implement to be registered with QEMU.
|
/// Trait exposed by all structs corresponding to QOM objects.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// - the struct must be `#[repr(C)]`
|
/// For classes declared in C:
|
||||||
///
|
///
|
||||||
/// - `Class` and `TYPE` must match the data in the `TypeInfo` (this is
|
/// - `Class` and `TYPE` must match the data in the `TypeInfo`;
|
||||||
/// automatic if the class is defined via `ObjectImpl`).
|
///
|
||||||
|
/// - the first field of the struct must be of the instance type corresponding
|
||||||
|
/// to the superclass, as declared in the `TypeInfo`
|
||||||
|
///
|
||||||
|
/// - likewise, the first field of the `Class` struct must be of the class type
|
||||||
|
/// corresponding to the superclass
|
||||||
|
///
|
||||||
|
/// For classes declared in Rust and implementing [`ObjectImpl`]:
|
||||||
|
///
|
||||||
|
/// - 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 declared as `PARENT_TYPE_NAME`
|
/// to the superclass, as declared in `ObjectImpl::PARENT_TYPE_NAME`
|
||||||
pub trait ObjectImpl: ClassInitImpl + Sized {
|
///
|
||||||
|
/// - likewise, the first field of the `Class` must be of the class struct
|
||||||
|
/// corresponding to the superclass
|
||||||
|
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;
|
||||||
|
|
||||||
/// The name of the type, which can be passed to `object_new()` to
|
/// The name of the type, which can be passed to `object_new()` to
|
||||||
/// generate an instance of this type.
|
/// generate an instance of this type.
|
||||||
const TYPE_NAME: &'static CStr;
|
const TYPE_NAME: &'static CStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait a type must implement to be registered with QEMU.
|
||||||
|
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>;
|
const PARENT_TYPE_NAME: Option<&'static CStr>;
|
||||||
|
|
|
@ -6,6 +6,7 @@ use std::{ffi::CStr, os::raw::c_void};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bindings::{self, DeviceClass, DeviceState, Error, ObjectClass, Property, VMStateDescription},
|
bindings::{self, DeviceClass, DeviceState, Error, ObjectClass, Property, VMStateDescription},
|
||||||
|
prelude::*,
|
||||||
zeroable::Zeroable,
|
zeroable::Zeroable,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -146,8 +147,8 @@ macro_rules! declare_properties {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// workaround until we can use --generate-cstr in bindgen.
|
unsafe impl ObjectType for bindings::DeviceState {
|
||||||
pub const TYPE_DEVICE: &CStr =
|
type Class = bindings::DeviceClass;
|
||||||
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_DEVICE) };
|
const TYPE_NAME: &'static CStr =
|
||||||
pub const TYPE_SYS_BUS_DEVICE: &CStr =
|
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_DEVICE) };
|
||||||
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
|
}
|
||||||
|
|
|
@ -6,3 +6,5 @@ pub use crate::bitops::IntegerExt;
|
||||||
|
|
||||||
pub use crate::cell::BqlCell;
|
pub use crate::cell::BqlCell;
|
||||||
pub use crate::cell::BqlRefCell;
|
pub use crate::cell::BqlRefCell;
|
||||||
|
|
||||||
|
pub use crate::definitions::ObjectType;
|
||||||
|
|
|
@ -2,11 +2,17 @@
|
||||||
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
|
// Author(s): Paolo Bonzini <pbonzini@redhat.com>
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
use std::ptr::addr_of;
|
use std::{ffi::CStr, ptr::addr_of};
|
||||||
|
|
||||||
pub use bindings::{SysBusDevice, SysBusDeviceClass};
|
pub use bindings::{SysBusDevice, SysBusDeviceClass};
|
||||||
|
|
||||||
use crate::{bindings, cell::bql_locked, irq::InterruptSource};
|
use crate::{bindings, cell::bql_locked, irq::InterruptSource, prelude::*};
|
||||||
|
|
||||||
|
unsafe impl ObjectType for SysBusDevice {
|
||||||
|
type Class = SysBusDeviceClass;
|
||||||
|
const TYPE_NAME: &'static CStr =
|
||||||
|
unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
|
||||||
|
}
|
||||||
|
|
||||||
impl SysBusDevice {
|
impl SysBusDevice {
|
||||||
/// Return `self` cast to a mutable pointer, for use in calls to C code.
|
/// Return `self` cast to a mutable pointer, for use in calls to C code.
|
||||||
|
|
|
@ -5,12 +5,8 @@
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
|
||||||
use qemu_api::{
|
use qemu_api::{
|
||||||
bindings::*,
|
bindings::*, c_str, declare_properties, define_property, definitions::ObjectImpl,
|
||||||
c_str, declare_properties, define_property,
|
device_class::DeviceImpl, impl_device_class, prelude::*, zeroable::Zeroable,
|
||||||
definitions::ObjectImpl,
|
|
||||||
device_class::{self, DeviceImpl},
|
|
||||||
impl_device_class,
|
|
||||||
zeroable::Zeroable,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -46,10 +42,15 @@ fn test_device_decl_macros() {
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl for DummyState {
|
unsafe impl ObjectType for DummyState {
|
||||||
type Class = DummyClass;
|
type Class = DummyClass;
|
||||||
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);
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for DummyState {
|
||||||
|
const PARENT_TYPE_NAME: Option<&'static CStr> =
|
||||||
|
Some(<DeviceState as ObjectType>::TYPE_NAME);
|
||||||
|
const ABSTRACT: bool = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceImpl for DummyState {
|
impl DeviceImpl for DummyState {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue