From: "Obei Sideg" <linux@obei.io>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
"Obei Sideg" <linux@obei.io>,
"Matt Gilbride" <mattgilbride@google.com>,
"Trevor Gross" <tmgross@umich.edu>
Subject: [PATCH] rust: types: Add `try_from_foreign()` method
Date: Tue, 23 Jan 2024 08:28:07 +0000 [thread overview]
Message-ID: <0100018d356f0fa5-1e771ce7-1787-4330-bc1e-0d7ce240288b-000000@email.amazonses.com> (raw)
In-Reply-To: 20240123082746.18284-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 {
Some(unsafe { T::from_foreign(p) })
}
``
Link: https://github.com/Rust-for-Linux/linux/issues/1057
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Matt Gilbride <mattgilbride@google.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Signed-off-by: Obei Sideg <linux@obei.io>
---
rust/kernel/types.rs | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index fdb778e65d79..cfee102ab7b9 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -46,6 +46,25 @@ 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 have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
+ /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
+ /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
+ /// this object must have been dropped.
+ unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
+ if ptr.is_null() {
+ None
+ } else {
+ Some(Self::from_foreign(ptr))
+ }
+ }
}
impl<T: 'static> ForeignOwnable for Box<T> {
--
2.39.2
next parent reply other threads:[~2024-01-23 8:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240123082746.18284-1-linux@obei.io>
2024-01-23 8:28 ` Obei Sideg [this message]
2024-01-23 12:09 ` [PATCH] rust: types: Add `try_from_foreign()` method Miguel Ojeda
[not found] <20240122162553.64610-1-linux@obei.io>
2024-01-22 16:26 ` Obei Sideg
2024-01-22 18:14 ` Martin Rodriguez Reboredo
2024-01-22 20:33 ` Trevor Gross
2024-01-22 22:01 ` Trevor Gross
2024-01-23 9:10 ` Jarkko Sakkinen
2024-01-22 21:14 ` Alice Ryhl
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=0100018d356f0fa5-1e771ce7-1787-4330-bc1e-0d7ce240288b-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=mattgilbride@google.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--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).