On Thu, Jul 10, 2025 at 4:48 PM Manos Pitsidianakis
<[email protected]> wrote:
>
> On Thu, Jul 10, 2025 at 5:26 PM Paolo Bonzini <[email protected]> wrote:
> >
> > On Thu, Jul 10, 2025 at 11:41 AM Manos Pitsidianakis
> > <[email protected]> wrote:
> > > > Aside from that, I actually liked using Device for the macro name in
> > > > your earlier versions.  Yes, it's just for properties in practice, but
> > > > it's nice and small to just say Device; and it mimics Object.  It's your
> > > > choice anyway.
> > >
> > > I was thinking of making a `Device` derive macro that lets you also
> > > define `DeviceImpl::REALIZE` and `DeviceImpl::vmsd` as macro
> > > attributes on the struct definition, then merge DeviceProperties into
> > > that. WDYT?
> >
> > Like #[derive(Device(realize = PL011State::realize))]? I kind of like
> > having traits for classes (the "const" does look a bit ugly/foreign,
> > but Linux has some other ideas using a #[vtable] procedural macro).
>
> I was thinking:
>
> #[repr(C)]
> #[derive(Device)]
> #[device(realize = PL011State::realize, vmsd = VMSTATE_PL011)]
> pub struct PL011State {
>   ..
> }
>
> I agree about traits for class methods, it's definitely cleaner. The
> lines blur here because we have REALIZE as a constant in order to make
> it nullable from the C side 🤔

Yes, I agree. Another factor to consider is the quality of error messages.

For what it's worth, this is the solution they use in Linux:
https://rust-for-linux.github.io/docs/macros/attr.vtable.html. In
short, they generate a HAS_REALIZE boolean const, and compute the
function pointer with something like
  if Self::HAS_REALIZE { Some(Self::realize) } else { None }

Forgetting the #[vtable] attribute on the "impl" produces a decent
error message, too.

And here is the source:
https://rust-for-linux.github.io/docs/src/macros/vtable.rs.html. Linux
doesn't use proc_macro2 or quote so probably it would have to be
rewritten for QEMU, but it's small and easy to understand.

Even if it's *also* not the nicest, it may be worth adopting this
convention just for consistency among mixed C/Rust programs.

Paolo


Paolo


Reply via email to