From: Mohamad Alsadhan Date: Tue, 28 Apr 2026 13:10:53 +0000 (+0100) Subject: rust: pin-init: extend `impl_zeroable_option` macro to handle generics X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=de54c2cb052952df2a6f91617471d1f02f213d4e;p=thirdparty%2Fkernel%2Flinux.git rust: pin-init: extend `impl_zeroable_option` macro to handle generics Improve impl_zeroable_option macro to handle generic impls for types like `&T`, `&mut T`, `NonNull`, and others (for which `Option` is guaranteed to be zeroable) with similar approach to `impl_zeroable`. Also, update old declarations to use generics e.g. `NonZeroU8` to `NonZero`. Signed-off-by: Mohamad Alsadhan Reviewed-by: Gary Guo Link: https://patch.msgid.link/20260428-pin-init-sync-v1-4-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 e34c9bdb88c39..9b76cf5597c62 100644 --- a/rust/pin-init/src/lib.rs +++ b/rust/pin-init/src/lib.rs @@ -1633,16 +1633,6 @@ 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);)* @@ -1669,17 +1659,26 @@ 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_zeroable_option { - ($($int:ty),* $(,)?) => { - // SAFETY: Safety comment written in the macro invocation. - $(unsafe impl ZeroableOption for $int {})* + ($($({$($generics:tt)*})? $t:ty, )*) => { + // SAFETY: Safety comments written in the macro invocation. + $(unsafe impl$($($generics)*)? ZeroableOption for $t {})* }; } impl_zeroable_option! { + // SAFETY: `Option<&T>` is part of the option layout optimization guarantee: + // . + {} &T, + // SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee: + // . + {} &mut T, + // SAFETY: `Option>` is part of the option layout optimization guarantee: + // . + {} NonNull, // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee: // ). - NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, - NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, + NonZero, NonZero, NonZero, NonZero, NonZero, NonZero, + NonZero, NonZero, NonZero, NonZero, NonZero, NonZero, } /// This trait allows creating an instance of `Self` which contains exactly one