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

@ -2,13 +2,14 @@
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
// SPDX-License-Identifier: GPL-2.0-or-later
use std::{ffi::CStr, os::raw::c_void};
use std::ffi::CStr;
use qemu_api::{
bindings::*,
c_str, declare_properties, define_property,
definitions::{ClassInitImpl, ObjectImpl},
device_class, device_class_init,
definitions::ObjectImpl,
device_class::{self, DeviceImpl},
impl_device_class,
zeroable::Zeroable,
};
@ -45,28 +46,23 @@ fn test_device_decl_macros() {
),
}
device_class_init! {
dummy_class_init,
props => DUMMY_PROPERTIES,
realize_fn => None,
legacy_reset_fn => None,
vmsd => VMSTATE,
}
impl ObjectImpl for DummyState {
type Class = DummyClass;
const TYPE_NAME: &'static CStr = c_str!("dummy");
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(device_class::TYPE_DEVICE);
}
impl ClassInitImpl for DummyState {
const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
Some(dummy_class_init);
const CLASS_BASE_INIT: Option<
unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
> = None;
impl DeviceImpl for DummyState {
fn properties() -> &'static [Property] {
&DUMMY_PROPERTIES
}
fn vmsd() -> Option<&'static VMStateDescription> {
Some(&VMSTATE)
}
}
impl_device_class!(DummyState);
unsafe {
module_call_init(module_init_type::MODULE_INIT_QOM);
object_unref(object_new(DummyState::TYPE_NAME.as_ptr()).cast());