mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
rust: types: make Opaque
be !Unpin
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 <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> Link: https://lore.kernel.org/r/20230630150216.109789-1-benno.lossin@proton.me Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
35cad617df
commit
0b4e3b6f6b
1 changed files with 14 additions and 5 deletions
|
@ -6,7 +6,7 @@ use crate::init::{self, PinInit};
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
use core::{
|
use core::{
|
||||||
cell::UnsafeCell,
|
cell::UnsafeCell,
|
||||||
marker::PhantomData,
|
marker::{PhantomData, PhantomPinned},
|
||||||
mem::MaybeUninit,
|
mem::MaybeUninit,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
ptr::NonNull,
|
ptr::NonNull,
|
||||||
|
@ -206,17 +206,26 @@ impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
|
||||||
///
|
///
|
||||||
/// This is meant to be used with FFI objects that are never interpreted by Rust code.
|
/// This is meant to be used with FFI objects that are never interpreted by Rust code.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
|
pub struct Opaque<T> {
|
||||||
|
value: UnsafeCell<MaybeUninit<T>>,
|
||||||
|
_pin: PhantomPinned,
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Opaque<T> {
|
impl<T> Opaque<T> {
|
||||||
/// Creates a new opaque value.
|
/// Creates a new opaque value.
|
||||||
pub const fn new(value: T) -> Self {
|
pub const fn new(value: T) -> Self {
|
||||||
Self(UnsafeCell::new(MaybeUninit::new(value)))
|
Self {
|
||||||
|
value: UnsafeCell::new(MaybeUninit::new(value)),
|
||||||
|
_pin: PhantomPinned,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an uninitialised value.
|
/// Creates an uninitialised value.
|
||||||
pub const fn uninit() -> Self {
|
pub const fn uninit() -> Self {
|
||||||
Self(UnsafeCell::new(MaybeUninit::uninit()))
|
Self {
|
||||||
|
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||||
|
_pin: PhantomPinned,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a pin-initializer from the given initializer closure.
|
/// Creates a pin-initializer from the given initializer closure.
|
||||||
|
@ -240,7 +249,7 @@ impl<T> Opaque<T> {
|
||||||
|
|
||||||
/// Returns a raw pointer to the opaque data.
|
/// Returns a raw pointer to the opaque data.
|
||||||
pub fn get(&self) -> *mut T {
|
pub fn get(&self) -> *mut T {
|
||||||
UnsafeCell::get(&self.0).cast::<T>()
|
UnsafeCell::get(&self.value).cast::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the value behind `this`.
|
/// Gets the value behind `this`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue