From: Wedson Almeida Filho Date: Mon, 30 Jan 2023 06:44:03 +0000 (-0300) Subject: rust: types: implement `ForeignOwnable` for the unit type X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=7118594466b8ed7285adb1dc177f2c84bfa33265;p=linux.git rust: types: implement `ForeignOwnable` for the unit type 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 Reviewed-by: Vincenzo Palazzo Reviewed-by: Gary Guo Reviewed-by: Andreas Hindborg Signed-off-by: Miguel Ojeda --- diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 411655eca3e92..9d0fdbc558433 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -85,6 +85,18 @@ impl ForeignOwnable for Box { } } +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.