From: Gary Guo <gary@garyguo.net>
To: Wedson Almeida Filho <wedsonaf@gmail.com>
Cc: rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type
Date: Fri, 27 Jan 2023 14:03:19 +0000 [thread overview]
Message-ID: <20230127140319.22af6060.gary@garyguo.net> (raw)
In-Reply-To: <20230119174036.64046-4-wedsonaf@gmail.com>
On Thu, 19 Jan 2023 14:40:35 -0300
Wedson Almeida Filho <wedsonaf@gmail.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@gmail.com>
> ---
> 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 _
this should really be `core::ptr::invalid(1)`. That's currently
unstable, but can be equivalently written as
`NonNull::<()>::dangling().as_ptr()`.
This has a different semantic meaning from `as` since it explicitly
suggests an invalid provenance and thus will not alias with other
pointers. (Although I don't think compiler currently can take advantage
of this fact yet)
> + }
> +
> + 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.
next prev parent reply other threads:[~2023-01-27 14:05 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-19 17:40 [PATCH 1/5] rust: types: introduce `ScopeGuard` Wedson Almeida Filho
2023-01-19 17:40 ` [PATCH 2/5] rust: types: introduce `ForeignOwnable` Wedson Almeida Filho
2023-01-20 19:59 ` Boqun Feng
2023-01-22 6:34 ` Wedson Almeida Filho
2023-01-27 13:55 ` Gary Guo
2023-01-30 5:59 ` Wedson Almeida Filho
2023-01-28 10:42 ` Vincenzo Palazzo
2023-01-28 14:53 ` Martin Rodriguez Reboredo
2023-01-28 17:05 ` Boqun Feng
2023-01-28 20:46 ` Martin Rodriguez Reboredo
2023-01-28 22:07 ` Boqun Feng
2023-01-29 3:52 ` Martin Rodriguez Reboredo
2023-01-19 17:40 ` [PATCH 3/5] rust: types: implement `ForeignOwnable` for `Box<T>` Wedson Almeida Filho
2023-01-27 13:56 ` Gary Guo
2023-01-28 10:50 ` Vincenzo Palazzo
2023-01-30 5:33 ` Alice Ferrazzi
2023-01-19 17:40 ` [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type Wedson Almeida Filho
2023-01-27 14:03 ` Gary Guo [this message]
2023-01-27 14:11 ` Miguel Ojeda
2023-01-28 11:13 ` Vincenzo Palazzo
2023-01-30 17:21 ` Miguel Ojeda
2023-01-30 5:55 ` Wedson Almeida Filho
2023-01-28 11:14 ` Vincenzo Palazzo
2023-01-19 17:40 ` [PATCH 5/5] rust: types: implement `ForeignOwnable` for `Arc<T>` Wedson Almeida Filho
2023-01-27 14:04 ` Gary Guo
2023-01-28 11:15 ` Vincenzo Palazzo
2023-01-30 5:35 ` Alice Ferrazzi
2023-01-20 6:23 ` [PATCH 1/5] rust: types: introduce `ScopeGuard` Boqun Feng
2023-01-22 6:31 ` Wedson Almeida Filho
2023-01-28 10:38 ` Vincenzo Palazzo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230127140319.22af6060.gary@garyguo.net \
--to=gary@garyguo.net \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).