[RFC PATCH 0/2] rust: Zeroable: allow struct update syntax outside init macros

Paolo Bonzini
2024-11-28T14:13:30Z

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