* Re: [PATCH] rust: types: Add `try_from_foreign()` method
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 21:14 ` Alice Ryhl
2 siblings, 0 replies; 8+ messages in thread
From: Martin Rodriguez Reboredo @ 2024-01-22 18:14 UTC (permalink / raw)
To: Obei Sideg, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux@vger.kernel.org,
Matt Gilbride
On 1/22/24 13:26, Obei Sideg wrote:
> 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: Rust-for-Linux/linux#1059
>
> Cc: Alice Ryhl <aliceryhl@google.com>
> Cc: Matt Gilbride <mattgilbride@google.com>
> Cc: Miguel Ojeda <ojeda@kernel.org>
> Signed-off-by: Obei Sideg <linux@obei.io>
> ---
> [...]
> + unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self>;
> }
>
> impl<T: 'static> ForeignOwnable for Box<T> {
> @@ -68,6 +80,14 @@ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
> // call to `Self::into_foreign`.
> unsafe { Box::from_raw(ptr as _) }
> }
> +
> + unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
> + if ptr.is_null() {
> + None
> + } else {
> + Some(Self::from_foreign(ptr))
> + }
> + }
> [...]
Shouldn't have `Box::try_from_foreign` been the default implementation
of `ForeignOwnable::try_from_foreign`?
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust: types: Add `try_from_foreign()` method
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
2 siblings, 2 replies; 8+ messages in thread
From: Trevor Gross @ 2024-01-22 20:33 UTC (permalink / raw)
To: Obei Sideg
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux@vger.kernel.org, Matt Gilbride
On Mon, Jan 22, 2024 at 2:11 PM Obei Sideg <linux@obei.io> wrote:
>
> 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:
Minor nit: commit messages are wrapped to 72 characters, looks like this is 50.
> ```rust
> // `p` is a maybe null pointer
> if p.is_null() {
> None
> } else {
> Some(unsafe { T::from_foreign(p) })
> }
> ``
>
> Link: Rust-for-Linux/linux#1059
You should link the issue
(https://github.com/Rust-for-Linux/linux/issues/1057) instead of or in
addition to the PR.
>
> Cc: Alice Ryhl <aliceryhl@google.com>
> Cc: Matt Gilbride <mattgilbride@google.com>
> Cc: Miguel Ojeda <ojeda@kernel.org>
> Signed-off-by: Obei Sideg <linux@obei.io>
Optional docs update below, in either case you may add:
Reviewed-by: Trevor Gross <tmgross@umich.edu>
> ---
> rust/kernel/types.rs | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index fdb778e65d79..946067cd9c00 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -46,6 +46,18 @@ 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.
> + ///
> + /// Like [`from_foreign`], but returns [`None`] if `ptr` is null.
Maybe
A convenience wrapper over [`from_foreign`] that returns [`None`]
if `ptr` is null.
Just to be clear that this introduces no additional functionality.
> + /// # 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>;
> }
>
> impl<T: 'static> ForeignOwnable for Box<T> {
> @@ -68,6 +80,14 @@ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
> // call to `Self::into_foreign`.
> unsafe { Box::from_raw(ptr as _) }
> }
> +
> + 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 ForeignOwnable for () {
> --
> 2.39.2
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust: types: Add `try_from_foreign()` method
2024-01-22 20:33 ` Trevor Gross
@ 2024-01-22 22:01 ` Trevor Gross
2024-01-23 9:10 ` Jarkko Sakkinen
1 sibling, 0 replies; 8+ messages in thread
From: Trevor Gross @ 2024-01-22 22:01 UTC (permalink / raw)
To: Obei Sideg
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux@vger.kernel.org, Matt Gilbride
On Mon, Jan 22, 2024 at 3:33 PM Trevor Gross <tmgross@umich.edu> wrote:
>
> On Mon, Jan 22, 2024 at 2:11 PM Obei Sideg <linux@obei.io> wrote:
> >
> > [...]
> >
> > Cc: Alice Ryhl <aliceryhl@google.com>
> > Cc: Matt Gilbride <mattgilbride@google.com>
> > Cc: Miguel Ojeda <ojeda@kernel.org>
> > Signed-off-by: Obei Sideg <linux@obei.io>
>
> Optional docs update below, in either case you may add:
>
> Reviewed-by: Trevor Gross <tmgross@umich.edu>
The others have a good point, please resend with a default
implementation on ForeignOwnable and I will re-review.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rust: types: Add `try_from_foreign()` method
2024-01-22 20:33 ` Trevor Gross
2024-01-22 22:01 ` Trevor Gross
@ 2024-01-23 9:10 ` Jarkko Sakkinen
1 sibling, 0 replies; 8+ messages in thread
From: Jarkko Sakkinen @ 2024-01-23 9:10 UTC (permalink / raw)
To: Trevor Gross, Obei Sideg
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux@vger.kernel.org, Matt Gilbride
On Mon Jan 22, 2024 at 8:33 PM UTC, Trevor Gross wrote:
> On Mon, Jan 22, 2024 at 2:11 PM Obei Sideg <linux@obei.io> wrote:
> >
> > 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:
>
> Minor nit: commit messages are wrapped to 72 characters, looks like this is 50.
It is actually 75 characters max:
"The body of the explanation, line wrapped at 75 columns, which will be
copied to the permanent changelog to describe this patch."
See https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format
BR, Jarkko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rust: types: Add `try_from_foreign()` method
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 21:14 ` Alice Ryhl
2 siblings, 0 replies; 8+ messages in thread
From: Alice Ryhl @ 2024-01-22 21:14 UTC (permalink / raw)
To: Obei Sideg, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux@vger.kernel.org,
Matt Gilbride
On 1/22/24 17:26, Obei Sideg wrote:
> + /// Tries to convert a foreign-owned object back to a Rust-owned one.
> + ///
> + /// Like [`from_foreign`], but 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>;
The safety comment should probably say "If `ptr` is not null, then ...".
> impl<T: 'static> ForeignOwnable for Box<T> {
> @@ -68,6 +80,14 @@ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
> // call to `Self::into_foreign`.
> unsafe { Box::from_raw(ptr as _) }
> }
> +
> + unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
> + if ptr.is_null() {
> + None
> + } else {
> + Some(Self::from_foreign(ptr))
> + }
> + }
I would just add a default implementation in the trait declaration. That
way, you can avoid adding the method to every implementation.
Alice
^ permalink raw reply [flat|nested] 8+ messages in thread