Skip to content

Commit 7fba725

Browse files
Darksonngregkh
authored andcommitted
rust: make UnsafeCell the outer type in Opaque
[ Upstream commit 35cad61 ] When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use `UnsafeCell` as the outer type. Intuitively, this is because a `MaybeUninit<T>` might not contain a `T`, but we always want the effect of the `UnsafeCell`, even if the inner value is uninitialized. Now, strictly speaking, this doesn't really make a difference. The compiler will always apply the `UnsafeCell` effect even if the inner value is uninitialized. But I think we should follow the convention here. Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Stable-dep-of: 0b4e3b6 ("rust: types: make `Opaque` be `!Unpin`") Signed-off-by: Sasha Levin <[email protected]>
1 parent 07256dc commit 7fba725

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

rust/kernel/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,17 @@ 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>(MaybeUninit<UnsafeCell<T>>);
209+
pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
210210

211211
impl<T> Opaque<T> {
212212
/// Creates a new opaque value.
213213
pub const fn new(value: T) -> Self {
214-
Self(MaybeUninit::new(UnsafeCell::new(value)))
214+
Self(UnsafeCell::new(MaybeUninit::new(value)))
215215
}
216216

217217
/// Creates an uninitialised value.
218218
pub const fn uninit() -> Self {
219-
Self(MaybeUninit::uninit())
219+
Self(UnsafeCell::new(MaybeUninit::uninit()))
220220
}
221221

222222
/// Creates a pin-initializer from the given initializer closure.
@@ -240,15 +240,15 @@ impl<T> Opaque<T> {
240240

241241
/// Returns a raw pointer to the opaque data.
242242
pub fn get(&self) -> *mut T {
243-
UnsafeCell::raw_get(self.0.as_ptr())
243+
UnsafeCell::get(&self.0).cast::<T>()
244244
}
245245

246246
/// Gets the value behind `this`.
247247
///
248248
/// This function is useful to get access to the value without creating intermediate
249249
/// references.
250250
pub const fn raw_get(this: *const Self) -> *mut T {
251-
UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
251+
UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
252252
}
253253
}
254254

0 commit comments

Comments
 (0)