Skip to content

Commit 87018c9

Browse files
committed
rust: Added Opaque::raw_get
This function does exactly the same thing as `UnsafeCell::raw_get` [1]. It exists to avoid creating intermediate references. Link: https://doc.rust-lang.org/core/cell/struct.UnsafeCell.html#method.raw_get [1] Signed-off-by: Benno Lossin <[email protected]>
1 parent 553a414 commit 87018c9

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

rust/kernel/types.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::{
1515
mem::MaybeUninit,
1616
ops::{self, Deref, DerefMut},
1717
pin::Pin,
18-
ptr::NonNull,
18+
ptr::{addr_of, NonNull},
1919
};
2020

2121
/// Permissions.
@@ -316,6 +316,18 @@ impl<T> Opaque<T> {
316316
pub fn get(&self) -> *mut T {
317317
UnsafeCell::raw_get(self.0.as_ptr())
318318
}
319+
320+
/// Gets the value behind `this`.
321+
///
322+
/// This function is useful to get access to the value without creating intermeditate
323+
/// references.
324+
///
325+
/// # Safety
326+
///
327+
/// The pointer supplied is valid.
328+
pub unsafe fn raw_get(this: *const Self) -> *mut T {
329+
UnsafeCell::raw_get(addr_of!((*this).0).cast::<UnsafeCell<T>>())
330+
}
319331
}
320332

321333
/// A bitmask.

0 commit comments

Comments
 (0)