public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: boqun.feng@gmail.com, ojeda@kernel.org, benno.lossin@proton.me,
	axboe@kernel.dk, tmgross@umich.edu, aliceryhl@google.com,
	bjorn3_gh@protonmail.com, gary@garyguo.net,
	alex.gaynor@gmail.com, a.hindborg@kernel.org
Subject: [RFC PATCH 0/2] rust: Zeroable: allow struct update syntax outside init macros
Date: Thu, 28 Nov 2024 15:13:21 +0100	[thread overview]
Message-ID: <20241128141323.481033-1-pbonzini@redhat.com> (raw)

The Zeroable trait is a marker trait, even though the various init macros
use a "fake" struct update syntax.  Sometimes, such a struct update
syntax can be useful even outside the init macros; therefore, this series
adds an associated const that returns an all-zero instance of a Zeroable type.

I'm sending this as RFC mostly because the diffstat is not too favorable.
This is mostly because patch 2 has to keep safety comments above the
"unsafe trait Zeroable" declarations.  It would be better if the trait
could be derived automatically, for example via "div rustbindgen" comments
(not my favorite syntax, and grossly underdocumented; but still).  That
would also remove "unsafe" altogether.

Nevertheless, it seems to me that this is a small improvement in
readability of the code that *uses* the structs, and it may be worth
considering it.

Another request for comments is whether the "..Zeroable::zeroed()" fake
struct update syntax used by the init macros should be changed to use
"..Zeroable::ZERO".  The trait does not reuse the init macro syntax,
because traits do not support const functions and it can be useful
to use Zeroable::ZERO in const context.

Personally I think it's not a problem, and decided to keep the two
spellings separate: "zeroed()" when working with the Init and PinInit
traits, and "ZERO" when working with the actual struct.  As far as I can
see, "..Zeroable::zeroed()" is unused in rust-dev, which makes it trivial
to switch.

Paolo


Paolo Bonzini (2):
  rust: Zeroable: allow struct update syntax outside init macros
  rust: block/mq: replace mem::zeroed() with Zeroable trait

 rust/kernel/block/mq/gen_disk.rs |  8 +++++---
 rust/kernel/block/mq/tag_set.rs  | 10 ++++++----
 rust/kernel/init.rs              |  7 ++++++-
 3 files changed, 18 insertions(+), 9 deletions(-)

-- 
2.47.0


             reply	other threads:[~2024-11-28 14:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-28 14:13 Paolo Bonzini [this message]
2024-11-28 14:13 ` [PATCH 1/2] rust: Zeroable: allow struct update syntax outside init macros Paolo Bonzini
2024-11-28 14:40   ` Alice Ryhl
2024-11-28 16:43     ` Paolo Bonzini
2024-11-29  9:39       ` Alice Ryhl
2024-11-28 14:13 ` [PATCH 2/2] rust: block/mq: replace mem::zeroed() with Zeroable trait Paolo Bonzini
2024-11-29  9:41   ` Alice Ryhl
2024-11-29 13:42     ` Paolo Bonzini

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=20241128141323.481033-1-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=axboe@kernel.dk \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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