Skip to content
/ linux Public
forked from torvalds/linux

Commit

Permalink
rust: types: implement ForeignOwnable for the unit type
Browse files Browse the repository at this point in the history
This allows us to use the unit type `()` when we have no object whose
ownership must be managed but one implementing the `ForeignOwnable`
trait is needed.

Signed-off-by: Wedson Almeida Filho <[email protected]>
Reviewed-by: Vincenzo Palazzo <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Reviewed-by: Andreas Hindborg <[email protected]>
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
wedsonaf authored and ojeda committed Feb 1, 2023
1 parent 26949ba commit 7118594
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ impl<T: 'static> ForeignOwnable for Box<T> {
}
}

impl ForeignOwnable for () {
type Borrowed<'a> = ();

fn into_foreign(self) -> *const core::ffi::c_void {
core::ptr::NonNull::dangling().as_ptr()
}

unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {}

unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}
}

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

0 comments on commit 7118594

Please sign in to comment.