Skip to content

Commit a58f5fa

Browse files
y86-devgregkh
authored andcommitted
rust: types: make Opaque be !Unpin
[ Upstream commit 0b4e3b6 ] Adds a `PhantomPinned` field to `Opaque<T>`. This removes the last Rust guarantee: the assumption that the type `T` can be freely moved. This is not the case for many types from the C side (e.g. if they contain a `struct list_head`). This change removes the need to add a `PhantomPinned` field manually to Rust structs that contain C structs which must not be moved. Signed-off-by: Benno Lossin <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 7fba725 commit a58f5fa

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

rust/kernel/types.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::init::{self, PinInit};
66
use alloc::boxed::Box;
77
use core::{
88
cell::UnsafeCell,
9-
marker::PhantomData,
9+
marker::{PhantomData, PhantomPinned},
1010
mem::MaybeUninit,
1111
ops::{Deref, DerefMut},
1212
ptr::NonNull,
@@ -206,17 +206,26 @@ impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
206206
///
207207
/// This is meant to be used with FFI objects that are never interpreted by Rust code.
208208
#[repr(transparent)]
209-
pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
209+
pub struct Opaque<T> {
210+
value: UnsafeCell<MaybeUninit<T>>,
211+
_pin: PhantomPinned,
212+
}
210213

211214
impl<T> Opaque<T> {
212215
/// Creates a new opaque value.
213216
pub const fn new(value: T) -> Self {
214-
Self(UnsafeCell::new(MaybeUninit::new(value)))
217+
Self {
218+
value: UnsafeCell::new(MaybeUninit::new(value)),
219+
_pin: PhantomPinned,
220+
}
215221
}
216222

217223
/// Creates an uninitialised value.
218224
pub const fn uninit() -> Self {
219-
Self(UnsafeCell::new(MaybeUninit::uninit()))
225+
Self {
226+
value: UnsafeCell::new(MaybeUninit::uninit()),
227+
_pin: PhantomPinned,
228+
}
220229
}
221230

222231
/// Creates a pin-initializer from the given initializer closure.
@@ -240,7 +249,7 @@ impl<T> Opaque<T> {
240249

241250
/// Returns a raw pointer to the opaque data.
242251
pub fn get(&self) -> *mut T {
243-
UnsafeCell::get(&self.0).cast::<T>()
252+
UnsafeCell::get(&self.value).cast::<T>()
244253
}
245254

246255
/// Gets the value behind `this`.

0 commit comments

Comments
 (0)