aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorBenno Lossin <lossin@kernel.org>2025-05-23 16:51:00 +0200
committerBenno Lossin <lossin@kernel.org>2025-06-11 21:13:56 +0200
commite93a238605348bc40fed77ba5582e311376d113b (patch)
tree08baf0c1e16fa34f184eb38135905e5d838b56ff
parentrust: pin-init: add `zeroed()` & `Zeroable::zeroed()` functions (diff)
downloadwireguard-linux-e93a238605348bc40fed77ba5582e311376d113b.tar.xz
wireguard-linux-e93a238605348bc40fed77ba5582e311376d113b.zip
rust: pin-init: implement `ZeroableOption` for `&T` and `&mut T`
`Option<&T>` and `Option<&mut T>` are documented [1] to have the `None` variant be all zeroes. Link: https://doc.rust-lang.org/stable/std/option/index.html#representation [1] Link: https://github.com/Rust-for-Linux/pin-init/pull/56/commits/5ef1638c79e019d3dc0c62db5905601644c2e60a Link: https://lore.kernel.org/all/20250523145125.523275-5-lossin@kernel.org Signed-off-by: Benno Lossin <lossin@kernel.org>
-rw-r--r--rust/pin-init/src/lib.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
index a5bb3939b58b..298a3e675b7f 100644
--- a/rust/pin-init/src/lib.rs
+++ b/rust/pin-init/src/lib.rs
@@ -1546,6 +1546,13 @@ pub unsafe trait ZeroableOption {}
// SAFETY: by the safety requirement of `ZeroableOption`, this is valid.
unsafe impl<T: ZeroableOption> Zeroable for Option<T> {}
+// SAFETY: `Option<&T>` is part of the option layout optimization guarantee:
+// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
+unsafe impl<T> ZeroableOption for &T {}
+// SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee:
+// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
+unsafe impl<T> ZeroableOption for &mut T {}
+
/// Create an initializer for a zeroed `T`.
///
/// The returned initializer will write `0x00` to every byte of the given `slot`.