rust: pin-init: Implement `Wrapper` for `UnsafePinned` behind feature flag.
authorChristian Schrefl <chrisi.schrefl@gmail.com>
Mon, 21 Apr 2025 22:18:06 +0000 (22:18 +0000)
committerBenno Lossin <benno.lossin@proton.me>
Thu, 1 May 2025 16:13:56 +0000 (18:13 +0200)
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 <chrisi.schrefl@gmail.com>
Link: https://github.com/Rust-for-Linux/pin-init/pull/37/commits/99cb1934425357e780ea5b0628f66633123847b8
[ Fixed commit authorship. - Benno ]
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
rust/pin-init/src/lib.rs

index 467ccc8bd61688c568ac0f9e3f94c12729d29067..745cf534d239cc22dbef2e10d964315b2d200c85 100644 (file)
 #![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<T> Wrapper<T> for MaybeUninit<T> {
         unsafe { cast_pin_init(value_init) }
     }
 }
+
+#[cfg(all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED))]
+impl<T> Wrapper<T> for core::pin::UnsafePinned<T> {
+    fn pin_init<E>(init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
+        // SAFETY: `UnsafePinned<T>` has a compatible layout to `T`.
+        unsafe { cast_pin_init(init) }
+    }
+}