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: [PATCH 1/2] rust: Zeroable: allow struct update syntax outside init macros
Date: Thu, 28 Nov 2024 15:13:22 +0100 [thread overview]
Message-ID: <20241128141323.481033-2-pbonzini@redhat.com> (raw)
In-Reply-To: <20241128141323.481033-1-pbonzini@redhat.com>
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. Add an associated
const that returns an all-zero instance of a Zeroable type.
The exact syntax used by the init macros cannot be reproduced without
forgoing the ability to use Zeroable::ZERO in const context. However,
it might not be a good idea to add a fn zeroed() inside the
Zeroable trait, to avoid confusion with the init::zeroed() function
and because Zeroable::ZERO is unrelated to the Init and PinInit
traits. In other words, let's treat this difference as a
feature rather than a bug.
The definition of the ZERO constant requires adding a Sized boundary, but
this is not a problem either because neither slices nor trait objects
are zeroable.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/kernel/init.rs | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletion(-)
diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
index a17ac8762d8f..a00e7ff6a513 100644
--- a/rust/kernel/init.rs
+++ b/rust/kernel/init.rs
@@ -1392,7 +1392,12 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
/// ```rust,ignore
/// let val: Self = unsafe { core::mem::zeroed() };
/// ```
-pub unsafe trait Zeroable {}
+pub unsafe trait Zeroable: Sized {
+ /// Return a value of Self whose memory representation consists of all zeroes.
+ // SAFETY: the Zeroable trait itself is unsafe, and declaring it (whether
+ // manually or via derivation) implies that this is not undefined behavior.
+ const ZERO: Self = unsafe { core::mem::zeroed() };
+}
/// Create a new zeroed T.
///
@@ -1444,7 +1444,7 @@ macro_rules! impl_zeroable {
{<T>} Opaque<T>,
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
- {<T: ?Sized + Zeroable>} UnsafeCell<T>,
+ {<T: Zeroable>} UnsafeCell<T>,
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
--
2.47.0
next prev parent 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 [RFC PATCH 0/2] rust: Zeroable: allow struct update syntax outside init macros Paolo Bonzini
2024-11-28 14:13 ` Paolo Bonzini [this message]
2024-11-28 14:40 ` [PATCH 1/2] " 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-2-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