From: Christian Schrefl Date: Mon, 21 Apr 2025 22:18:06 +0000 (+0000) Subject: rust: pin-init: Implement `Wrapper` for `UnsafePinned` behind feature flag. X-Git-Tag: v6.16-rc1~45^2~34^2~7 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=b862aac8fd46601fa20226c9f5d6c6d308678b4d;p=linux-block.git rust: pin-init: Implement `Wrapper` for `UnsafePinned` behind feature flag. Add the `unsafe-pinned` feature which gates the `Wrapper` implementation of the `core::pin::UnsafePinned` struct. For now this is just a cargo feature, but once `core::pin::UnsafePinned` is stable a config flag can be added to allow the usage of this implementation in the linux kernel. Signed-off-by: Christian Schrefl Link: https://github.com/Rust-for-Linux/pin-init/pull/37/commits/99cb1934425357e780ea5b0628f66633123847b8 [ Fixed commit authorship. - Benno ] Signed-off-by: Benno Lossin --- diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs index 467ccc8bd616..745cf534d239 100644 --- a/rust/pin-init/src/lib.rs +++ b/rust/pin-init/src/lib.rs @@ -269,6 +269,10 @@ #![forbid(missing_docs, unsafe_op_in_unsafe_fn)] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "alloc", feature(allocator_api))] +#![cfg_attr( + all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED), + feature(unsafe_pinned) +)] use core::{ cell::UnsafeCell, @@ -1557,3 +1561,11 @@ impl Wrapper for MaybeUninit { unsafe { cast_pin_init(value_init) } } } + +#[cfg(all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED))] +impl Wrapper for core::pin::UnsafePinned { + fn pin_init(init: impl PinInit) -> impl PinInit { + // SAFETY: `UnsafePinned` has a compatible layout to `T`. + unsafe { cast_pin_init(init) } + } +}