Skip to content

Commit de17678

Browse files
committed
Implement PinInitable for UnsafePinned behind feature flag.
Add the `unsafe-pinned` feature which gates the `PinInitable` 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>
1 parent 3680171 commit de17678

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pin-init-internal = { path = "./internal", version = "=0.0.5" }
2121
default = ["std", "alloc"]
2222
std = []
2323
alloc = []
24+
unsafe-pinned = []
2425

2526
[build-dependencies]
2627
rustc_version = "0.4"

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_version::{version, Version};
33
fn main() {
44
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
55
println!("cargo::rustc-check-cfg=cfg(RUSTC_NEW_UNINIT_IS_STABLE)");
6+
println!("cargo::rustc-check-cfg=cfg(RUSTC_HAS_UNSAFE_PINNED)");
67
if version().unwrap() >= Version::parse("1.81.0").unwrap()
78
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
89
{
@@ -11,4 +12,7 @@ fn main() {
1112
if version().unwrap() >= Version::parse("1.82.0").unwrap() {
1213
println!("cargo:rustc-cfg=RUSTC_NEW_UNINIT_IS_STABLE");
1314
}
15+
if version().unwrap() >= Version::parse("1.88.0-nightly").unwrap() {
16+
println!("cargo:rustc-cfg=RUSTC_HAS_UNSAFE_PINNED");
17+
}
1418
}

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@
269269
#![forbid(missing_docs, unsafe_op_in_unsafe_fn)]
270270
#![cfg_attr(not(feature = "std"), no_std)]
271271
#![cfg_attr(feature = "alloc", feature(allocator_api))]
272+
#![cfg_attr(
273+
all(feature = "unsafe-pinned", RUSTC_HAS_UNSAFE_PINNED),
274+
feature(unsafe_pinned)
275+
)]
272276

273277
use core::{
274278
cell::UnsafeCell,
@@ -1556,3 +1560,12 @@ impl<T> PinInitable<T> for MaybeUninit<T> {
15561560
unsafe { cast_pin_init(value_init) }
15571561
}
15581562
}
1563+
1564+
#[cfg(all(feature = "unsafe-pinned", RUSTC_HAS_UNSAFE_PINNED))]
1565+
impl<T> PinInitable<T> for core::pin::UnsafePinned<T> {
1566+
fn pin_init<E>(init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
1567+
// SAFETY:
1568+
// `UnsafePinned<T>` has a compatible layout to `T`.
1569+
unsafe { cast_pin_init(init) }
1570+
}
1571+
}

0 commit comments

Comments
 (0)