Re: [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type

From: Vincenzo Palazzo
Date: Sat Jan 28 2023 - 06:14:38 EST


On Thu Jan 19, 2023 at 6:40 PM CET, Wedson Almeida Filho wrote:
> 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 <wedsonaf@xxxxxxxxx>

Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@xxxxxxxxx>

> ---
> rust/kernel/types.rs | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index e037c262f23e..8f80cffbff59 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> {
> }
> }
>
> +impl ForeignOwnable for () {
> + type Borrowed<'a> = ();
> +
> + fn into_foreign(self) -> *const core::ffi::c_void {
> + // We use 1 to be different from a null pointer.
> + 1usize as _
> + }
> +
> + 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.
> --
> 2.34.1