mbox series

[0/8] rust: qom: move bridge for TypeInfo and DeviceClass functions to common code

Message ID 20241125080507.115450-1-pbonzini@redhat.com (mailing list archive)
Headers show
Series rust: qom: move bridge for TypeInfo and DeviceClass functions to common code | expand

Message

Paolo Bonzini Nov. 25, 2024, 8:04 a.m. UTC
This is a first step towards modeling QOM in the Rust type system.
This is by no means the final state of the bindings; rather, the
one thing it focuses on is moving unsafe code out of devices.  This
in principle removes code duplication, though with only one device
the duplication is still theoretical.

In particular the pl011 device includes bridge functions for instance_init,
DeviceClass::realize and DeviceClass::unrealize that take raw pointers
and call to (usually safe, though not always) functions that take Rust
references.

These functions would be the same for all devices; parameterize them on
a generic type, so that the implementation of TypeInfo.class_init can
reuse them.  Likewise implement TypeInfo.instance_finalize simply as
std::ptr::drop_in_place, which automatically bridges to the Drop trait
if the class implements it.

Some of the traits resemble those in Manos's implementation at
https://lore.kernel.org/qemu-devel/20241024-rust-round-2-v1-3-051e7a25b978@linaro.org/;
in particular DeviceImpl is an extension of his same-named trait, also
covering properties and vmstate, and ClassInitImpl for now is the same
as his ClassImplUnsafe.  I have not added an explicit "Unsafe" suffix
because I don't plan to have separate safe and unsafe versions of the
traits, just like there is no DeviceImplUnsafe.  The main difference
lies in the usage of generics and monomorphization over macros;
generics are always compiled and any compiler error messages point
at the implementation rather than the invocation of the macro.

While at it, the series starts adding documentation to the QOM machinery.
A lot of the extra lines of code in fact is documentation and/or safety
comments.

Paolo

Paolo Bonzini (8):
  rust: qom: add default definitions for ObjectImpl
  rust: qom: rename Class trait to ClassInitImpl
  rust: qom: convert type_info! macro to an associated const
  rust: qom: move ClassInitImpl to the instance side
  rust: qdev: move device_class_init! body to generic function,
    ClassInitImpl implementation to macro
  rust: qdev: move bridge for realize and reset functions out of pl011
  rust: qom: automatically use Drop trait to implement instance_finalize
  rust: qom: move bridge for TypeInfo functions out of pl011

 rust/hw/char/pl011/src/device.rs       |  85 ++++++---------
 rust/hw/char/pl011/src/device_class.rs |  34 ------
 rust/qemu-api/src/definitions.rs       | 140 ++++++++++++++++++-------
 rust/qemu-api/src/device_class.rs      | 114 +++++++++++++++++---
 rust/qemu-api/tests/tests.rs           |  33 +++---
 5 files changed, 246 insertions(+), 160 deletions(-)