[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230130184151.651303ad.gary@garyguo.net>
Date: Mon, 30 Jan 2023 18:41:51 +0000
From: Gary Guo <gary@...yguo.net>
To: Wedson Almeida Filho <wedsonaf@...il.com>
Cc: rust-for-linux@...r.kernel.org, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
linux-kernel@...r.kernel.org,
Vincenzo Palazzo <vincenzopalazzodev@...il.com>
Subject: Re: [PATCH v2 4/5] rust: types: implement `ForeignOwnable` for the
unit type
On Mon, 30 Jan 2023 03:44:03 -0300
Wedson Almeida Filho <wedsonaf@...il.com> 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@...il.com>
> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@...il.com>
Reviewed-by: Gary Guo <gary@...yguo.net>
> ---
> v1 -> v2: use `NonNull::dangling` to generate pointer
>
> rust/kernel/types.rs | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 411655eca3e9..9d0fdbc55843 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -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.
Powered by blists - more mailing lists