Rust-for-linux archive mirror
 help / color / mirror / Atom feed
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 v4] rust: types: Add `try_from_foreign()` method
Date: Sun, 28 Jan 2024 06:47:09 +0000	[thread overview]
Message-ID: <0100018d4ed26b78-7c0d5d0c-cf92-48ff-9f97-a656a9560e2b-000000@email.amazonses.com> (raw)
In-Reply-To: <CAH5fLghtZK94Co2+0agG91SZQrqke8H=oyHfmRxi2KbRTPJp2g@mail.gmail.com>

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 null if `ptr`
is null, otherwsie return `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>
---
 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-28  6:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240124113241.380026-1-linux@obei.io>
2024-01-24 11:32 ` [PATCH v3] rust: types: Add `try_from_foreign()` method Obei Sideg
2024-01-24 11:53   ` Alice Ryhl
     [not found]     ` <20240124141632.546912-1-linux@obei.io>
2024-01-24 14:17       ` Obei Sideg
2024-01-24 14:18         ` Alice Ryhl
     [not found]     ` <20240128064656.6039-1-linux@obei.io>
2024-01-28  6:47       ` Obei Sideg [this message]
2024-01-24 15:36   ` Boqun Feng
     [not found] <20240128065550.7373-1-linux@obei.io>
2024-01-28  6:56 ` [PATCH v4] " Obei Sideg

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=0100018d4ed26b78-7c0d5d0c-cf92-48ff-9f97-a656a9560e2b-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).