From: Mohamad Alsadhan Date: Tue, 28 Apr 2026 13:10:52 +0000 (+0100) Subject: rust: pin-init: cleanup `Zeroable` and `ZeroableOptions` X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04828a538da2bed3150116929306f849a3337a37;p=thirdparty%2Fkernel%2Flinux.git rust: pin-init: cleanup `Zeroable` and `ZeroableOptions` Place definitions and implementations (incl. macro invocations) of the `Zeroable` trait first in the relevant section of `src/lib.rs`, followed by the ZeroableOption trait and its implementations. Rename `impl_non_zero_int_zeroable_option` to `impl_zeroable_option` for consistency. This commit should not introduce any functional changes. Signed-off-by: Mohamad Alsadhan Reviewed-by: Gary Guo Link: https://patch.msgid.link/20260428-pin-init-sync-v1-3-07f9bd3859fb@garyguo.net Signed-off-by: Gary Guo --- diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs index 4f50994bd2687..e34c9bdb88c39 100644 --- a/rust/pin-init/src/lib.rs +++ b/rust/pin-init/src/lib.rs @@ -1517,27 +1517,6 @@ pub unsafe trait Zeroable { } } -/// Marker trait for types that allow `Option` to be set to all zeroes in order to write -/// `None` to that location. -/// -/// # Safety -/// -/// The implementer needs to ensure that `unsafe impl Zeroable for Option {}` is sound. -pub unsafe trait ZeroableOption {} - -// SAFETY: by the safety requirement of `ZeroableOption`, this is valid. -unsafe impl Zeroable for Option {} - -// SAFETY: `Option<&T>` is part of the option layout optimization guarantee: -// . -unsafe impl ZeroableOption for &T {} -// SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee: -// . -unsafe impl ZeroableOption for &mut T {} -// SAFETY: `Option>` is part of the option layout optimization guarantee: -// . -unsafe impl ZeroableOption for NonNull {} - /// Create an initializer for a zeroed `T`. /// /// The returned initializer will write `0x00` to every byte of the given `slot`. @@ -1643,6 +1622,27 @@ macro_rules! impl_tuple_zeroable { impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J); +/// Marker trait for types that allow `Option` to be set to all zeroes in order to write +/// `None` to that location. +/// +/// # Safety +/// +/// The implementer needs to ensure that `unsafe impl Zeroable for Option {}` is sound. +pub unsafe trait ZeroableOption {} + +// SAFETY: by the safety requirement of `ZeroableOption`, this is valid. +unsafe impl Zeroable for Option {} + +// SAFETY: `Option<&T>` is part of the option layout optimization guarantee: +// . +unsafe impl ZeroableOption for &T {} +// SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee: +// . +unsafe impl ZeroableOption for &mut T {} +// SAFETY: `Option>` is part of the option layout optimization guarantee: +// . +unsafe impl ZeroableOption for NonNull {} + macro_rules! impl_fn_zeroable_option { ([$($abi:literal),* $(,)?] $args:tt) => { $(impl_fn_zeroable_option!({extern $abi} $args);)* @@ -1668,14 +1668,14 @@ macro_rules! impl_fn_zeroable_option { impl_fn_zeroable_option!(["Rust", "C"] { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U }); -macro_rules! impl_non_zero_int_zeroable_option { +macro_rules! impl_zeroable_option { ($($int:ty),* $(,)?) => { // SAFETY: Safety comment written in the macro invocation. $(unsafe impl ZeroableOption for $int {})* }; } -impl_non_zero_int_zeroable_option! { +impl_zeroable_option! { // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee: // ). NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,