From: "Obei Sideg" <linux@obei.io>
To: "aliceryhl@google.com" <aliceryhl@google.com>
Cc: "a.hindborg@samsung.com" <a.hindborg@samsung.com>,
"alex.gaynor@gmail.com" <alex.gaynor@gmail.com>,
"benno.lossin@proton.me" <benno.lossin@proton.me>,
"bjorn3_gh@protonmail.com" <bjorn3_gh@protonmail.com>,
"boqun.feng@gmail.com" <boqun.feng@gmail.com>,
"gary@garyguo.net" <gary@garyguo.net>,
"Obei Sideg" <linux@obei.io>,
"ojeda@kernel.org" <ojeda@kernel.org>,
"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
"wedsonaf@gmail.com" <wedsonaf@gmail.com>
Subject: [PATCH v5] rust: types: Add `try_from_foreign()` method
Date: Mon, 29 Jan 2024 06:19:00 +0000 [thread overview]
Message-ID: <0100018d53df01f6-18506de0-811f-45fb-83f0-d64ec8a9d594-000000@email.amazonses.com> (raw)
In-Reply-To: 20240129061852.4469-1-linux@obei.io
Currently `ForeignOwnable::from_foreign()` only works for non-null
pointers for the existing impls (e.g. Box, Arc). It may create a few
duplicate code like:
```rust
// `p` is a maybe null pointer
if p.is_null() {
None
} else {
unsafe { Some(Self::from_foreign(ptr)) }
}
```
Adding a `try_from_foreign()` method that will return `None` if `ptr`
is null, otherwsie return `Some(from_foreign(ptr))`.
Link: https://github.com/Rust-for-Linux/linux/issues/1057
Signed-off-by: Obei Sideg <linux@obei.io>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Obei Sideg <linux@obei.io>
---
rust/kernel/types.rs | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index fdb778e65d79..04a356ec3534 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -46,6 +46,23 @@ pub trait ForeignOwnable: Sized {
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
/// this object must have been dropped.
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
+
+ /// Tries to convert a foreign-owned object back to a Rust-owned one.
+ ///
+ /// A convenience wrapper over [`from_foreign`] that returns [`None`] if `ptr` is null.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must either be null or satisfy the safety requirements for [`from_foreign`].
+ unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
+ if ptr.is_null() {
+ None
+ } else {
+ // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
+ // call to [`into_foreign`].
+ unsafe { Some(Self::from_foreign(ptr)) }
+ }
+ }
}
impl<T: 'static> ForeignOwnable for Box<T> {
--
2.39.2
parent reply other threads:[~2024-01-29 6:19 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <20240129061852.4469-1-linux@obei.io>]
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=0100018d53df01f6-18506de0-811f-45fb-83f0-d64ec8a9d594-000000@email.amazonses.com \
--to=linux@obei.io \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--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 read-only IMAP folder(s) and NNTP newsgroup(s).