mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
	david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org,
	kwilczynski@kernel.org, bhelgaas@google.com,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-pci@vger.kernel.org
Subject: Re: [PATCH v4 4/5] rust: types: ForeignOwnable: Add type Target
Date: Thu, 26 Jun 2025 13:20:55 -0700	[thread overview]
Message-ID: <aF2rpzSccqgoVvn0@tardis.local> (raw)
In-Reply-To: <20250626200054.243480-5-dakr@kernel.org>

On Thu, Jun 26, 2025 at 10:00:42PM +0200, Danilo Krummrich wrote:
> ForeignOwnable::Target defines the payload data of a ForeignOwnable. For
> Arc<T> for instance, ForeignOwnable::Target would just be T.
> 
> This is useful for cases where a trait bound is required on the target
> type of the ForeignOwnable. For instance:
> 
> 	fn example<P>(data: P)
> 	   where
> 	      P: ForeignOwnable,
> 	      P::Target: MyTrait,
> 	{}
> 
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Boqun Feng <boqun.feng@gmail.com>

One nit below:

> ---
>  rust/kernel/alloc/kbox.rs | 2 ++
>  rust/kernel/sync/arc.rs   | 1 +
>  rust/kernel/types.rs      | 4 ++++
>  3 files changed, 7 insertions(+)
> 
> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> index c386ff771d50..66fad9777567 100644
> --- a/rust/kernel/alloc/kbox.rs
> +++ b/rust/kernel/alloc/kbox.rs
> @@ -403,6 +403,7 @@ unsafe impl<T: 'static, A> ForeignOwnable for Box<T, A>
>  where
>      A: Allocator,
>  {
> +    type Target = T;
>      type PointedTo = T;
>      type Borrowed<'a> = &'a T;
>      type BorrowedMut<'a> = &'a mut T;
> @@ -435,6 +436,7 @@ unsafe impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
>  where
>      A: Allocator,
>  {
> +    type Target = T;
>      type PointedTo = T;
>      type Borrowed<'a> = Pin<&'a T>;
>      type BorrowedMut<'a> = Pin<&'a mut T>;
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index c7af0aa48a0a..24fb63597d35 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -374,6 +374,7 @@ pub fn into_unique_or_drop(self) -> Option<Pin<UniqueArc<T>>> {
>  
>  // SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
>  unsafe impl<T: 'static> ForeignOwnable for Arc<T> {
> +    type Target = T;
>      type PointedTo = ArcInner<T>;
>      type Borrowed<'a> = ArcBorrow<'a, T>;
>      type BorrowedMut<'a> = Self::Borrowed<'a>;
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 3958a5f44d56..74c787b352a9 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -27,6 +27,9 @@
>  /// [`into_foreign`]: Self::into_foreign
>  /// [`PointedTo`]: Self::PointedTo
>  pub unsafe trait ForeignOwnable: Sized {
> +    /// The payload type of the foreign-owned value.
> +    type Target;

I think `ForeignOwnable` also implies a `T` maybe get dropped via a
pointer from `into_foreign()`. Not sure it's worth mentioning though.

Regards,
Boqun

> +
>      /// Type used when the value is foreign-owned. In practical terms only defines the alignment of
>      /// the pointer.
>      type PointedTo;
> @@ -128,6 +131,7 @@ unsafe fn try_from_foreign(ptr: *mut Self::PointedTo) -> Option<Self> {
>  
>  // SAFETY: The `into_foreign` function returns a pointer that is dangling, but well-aligned.
>  unsafe impl ForeignOwnable for () {
> +    type Target = ();
>      type PointedTo = ();
>      type Borrowed<'a> = ();
>      type BorrowedMut<'a> = ();
> -- 
> 2.49.0
> 

  reply	other threads:[~2025-06-26 20:20 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-26 20:00 [PATCH v4 0/5] Improvements for Devres Danilo Krummrich
2025-06-26 20:00 ` [PATCH v4 1/5] rust: revocable: support fallible PinInit types Danilo Krummrich
2025-06-26 20:00 ` [PATCH v4 2/5] rust: devres: replace Devres::new_foreign_owned() Danilo Krummrich
2025-06-26 20:00 ` [PATCH v4 3/5] rust: devres: get rid of Devres' inner Arc Danilo Krummrich
2025-06-26 20:14   ` Boqun Feng
2025-06-26 23:33   ` Benno Lossin
2025-06-26 23:53     ` Danilo Krummrich
2025-06-27  9:01       ` Benno Lossin
2025-06-27 19:59   ` Benno Lossin
2025-06-26 20:00 ` [PATCH v4 4/5] rust: types: ForeignOwnable: Add type Target Danilo Krummrich
2025-06-26 20:20   ` Boqun Feng [this message]
2025-06-26 23:17     ` Benno Lossin
2025-06-26 23:21       ` Boqun Feng
2025-06-26 23:36         ` Benno Lossin
2025-06-26 23:45           ` Boqun Feng
2025-06-26 23:55             ` Boqun Feng
2025-06-27  9:05               ` Benno Lossin
2025-06-26 23:22   ` Benno Lossin
2025-06-27 19:53   ` Miguel Ojeda
2025-07-01 11:49   ` Alice Ryhl
2025-06-26 20:00 ` [PATCH v4 5/5] rust: devres: implement register_release() Danilo Krummrich
2025-06-26 20:37   ` Boqun Feng
2025-06-26 20:48     ` Danilo Krummrich
2025-06-26 21:16       ` Boqun Feng
2025-06-26 21:20         ` Danilo Krummrich
2025-06-26 21:21           ` Boqun Feng
2025-06-26 23:19       ` Benno Lossin
2025-06-27 22:06         ` Boqun Feng
2025-06-28  6:06           ` Benno Lossin
2025-06-28  6:38             ` Boqun Feng
2025-06-28  7:53               ` Benno Lossin
2025-06-28  9:58                 ` Danilo Krummrich
2025-06-28 12:13                   ` Benno Lossin
2025-06-28 12:32                     ` Danilo Krummrich
2025-06-29 15:11 ` [PATCH v4 0/5] Improvements for Devres Danilo Krummrich

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=aF2rpzSccqgoVvn0@tardis.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@kernel.org \
    --cc=david.m.ertman@intel.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=ira.weiny@intel.com \
    --cc=kwilczynski@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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

all inboxes | Powered by JetHome®