qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com, junjie.mao@hotmail.com
Subject: [RFC PATCH 0/9] rust: (mostly) type safe VMState
Date: Tue, 31 Dec 2024 01:23:27 +0100	[thread overview]
Message-ID: <20241231002336.25931-1-pbonzini@redhat.com> (raw)

The existing translation of the C macros for vmstate does not make
any attempt to type-check vmstate declarations against the struct, so
introduce a new system that computes VMStateField based on the actual
struct declaration.

Macros do not have full access to the type system, therefore a full
implementation of this scheme requires a helper trait to analyze the type
and produce a VMStateField from it; the trait stores the VMStateField
as ana associated const.  Then, a macro "vmstate_of!" accepts arguments
similar to "offset_of!" and tricks the compiler into looking up the
trait for the right type.

Structs and cells have their own sub-VMStateDescription.  This poses
a problem in that references can only be included in associated consts
starting with Rust 1.83.0, when the const_refs_static was stabilized.
So for now structs and cells cannot be done in a type-safe manner,
but arrays, pointers and scalars can.

Technically, scalar types have the same issue in that they point to a
VMStateInfo.  For this case however there is onlyya limited list of
VMStateInfos, so I am placing them in an enum, and going from enum
to &VMStateInfo only when building the VMStateField (i.e. in a static's
value rather than in an associated const, and in a macro rather than in
a const fn).  This isn't great, but it's easily removed when QEMU starts
accepting a newer Rust version.

The series also introduces the pattern of applying changes to a struct
using methods with a fn(mut self) -> Self signature, for example

    pub const fn with_version_id(mut self, version_id: i32) -> Self {
        self.version_id = version_id;
        self
    }

This is not quite the builder pattern because there is no need for a
final ".build()" call, but it's similar/related.

The unfavorable diffstat comes half from the const_refs_static workaround,
half from new documentation (so in that case it's a plus rather than
a minus).  And also, there is more functionality and extensibility in
the new mechanism, whereas only a subset of the C macros was included
in the previous implementation.  The exception is "field_exists", which
may be added later if needed as a modifier, similar to with_version_id().

Paolo


Paolo Bonzini (9):
  rust: vmstate: add new type safe implementation
  rust: vmstate: implement VMState for non-leaf types
  rust: vmstate: add varray support to vmstate_of!
  rust: vmstate: implement Zeroable for VMStateField
  rust: vmstate: implement VMState for scalar types
  rust: vmstate: add public utility macros to implement VMState
  rust: qemu_api: add vmstate_struct and vmstate_cell
  rust: pl011: switch vmstate to new-style macros
  rust: vmstate: remove translation of C vmstate macros

 rust/hw/char/pl011/src/device.rs       |   3 +-
 rust/hw/char/pl011/src/device_class.rs |  36 +-
 rust/hw/char/pl011/src/lib.rs          |   6 +
 rust/qemu-api/src/prelude.rs           |   2 +
 rust/qemu-api/src/vmstate.rs           | 673 ++++++++++++++++---------
 rust/qemu-api/src/zeroable.rs          |  31 ++
 6 files changed, 481 insertions(+), 270 deletions(-)

-- 
2.47.1



             reply	other threads:[~2024-12-31  0:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-31  0:23 Paolo Bonzini [this message]
2024-12-31  0:23 ` [RFC PATCH 1/9] rust: vmstate: add new type safe implementation Paolo Bonzini
2025-01-07  8:58   ` Zhao Liu
2025-01-07 12:23     ` Paolo Bonzini
2025-01-07 14:01       ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 2/9] rust: vmstate: implement VMState for non-leaf types Paolo Bonzini
2025-01-07 15:43   ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 3/9] rust: vmstate: add varray support to vmstate_of! Paolo Bonzini
2025-01-08  3:28   ` Zhao Liu
2025-01-15 10:14     ` Paolo Bonzini
2024-12-31  0:23 ` [RFC PATCH 4/9] rust: vmstate: implement Zeroable for VMStateField Paolo Bonzini
2025-01-06 14:31   ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 5/9] rust: vmstate: implement VMState for scalar types Paolo Bonzini
2025-01-08  6:45   ` Zhao Liu
2025-01-15 13:08     ` Paolo Bonzini
2025-01-16  6:59       ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 6/9] rust: vmstate: add public utility macros to implement VMState Paolo Bonzini
2025-01-08  8:15   ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 7/9] rust: qemu_api: add vmstate_struct and vmstate_cell Paolo Bonzini
2025-01-07 16:49   ` Paolo Bonzini
2024-12-31  0:23 ` [RFC PATCH 8/9] rust: pl011: switch vmstate to new-style macros Paolo Bonzini
2025-01-08  8:27   ` Zhao Liu
2024-12-31  0:23 ` [RFC PATCH 9/9] rust: vmstate: remove translation of C vmstate macros Paolo Bonzini
2025-01-08  8:40   ` Zhao Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241231002336.25931-1-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=junjie.mao@hotmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-rust@nongnu.org \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).