Skip to content

Commit

Permalink
rust: add PointerWrapper implementation for *mut T.
Browse files Browse the repository at this point in the history
The implementation is straightforward: converting to `void *` and
borrowing are just pointer casts.

This is used by the GPIO subsystem where some context data is controlled
by the C side, so it is necessarily a raw pointer. (This is hidden from
driver writers though.)

Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
  • Loading branch information
wedsonaf committed Nov 17, 2021
1 parent 3cc55c1 commit 21a85a1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ impl<T: PointerWrapper + Deref> PointerWrapper for Pin<T> {
}
}

impl<T> PointerWrapper for *mut T {
type Borrowed<'a> = *mut T;

fn into_pointer(self) -> *const c_types::c_void {
self as _
}

unsafe fn borrow<'a>(ptr: *const c_types::c_void) -> Self::Borrowed<'a> {
ptr as _
}

unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self {
ptr as _
}
}

/// Runs a cleanup function/closure when dropped.
///
/// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
Expand Down

0 comments on commit 21a85a1

Please sign in to comment.