From: Danilo Krummrich <dakr@kernel.org>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Alexandre Courbot" <acourbot@nvidia.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U
Date: Tue, 8 Apr 2025 15:03:13 +0200 [thread overview]
Message-ID: <Z_UekWa9F8w_tmuy@pollux> (raw)
In-Reply-To: <D916LG7Z9Q31.5RVNMYM38E2D@proton.me>
On Tue, Apr 08, 2025 at 10:22:29AM +0000, Benno Lossin wrote:
> On Tue Apr 8, 2025 at 7:18 AM CEST, Alexandre Courbot wrote:
> > This enables the creation of trait objects backed by a Box, similarly to
> > what can be done with the standard library.
> >
> > Suggested-by: Benno Lossin <benno.lossin@proton.me>
> > Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> > ---
> > From this discussion on Zulip [1].
> >
> > Heavily inspired from the similar feature on `Arc`.
> >
> > [1] https://rust-for-linux.zulipchat.com/#narrow/channel/291565-Help/topic/Trait.20objects.3F/with/510689662
> > ---
> > rust/kernel/alloc/kbox.rs | 14 +++++++++++++-
> > 1 file changed, 13 insertions(+), 1 deletion(-)
> >
> > diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> > index b77d32f3a58bab5ec73c612bdaaba0d79bfdff65..969b9f9fd3149685e1d1ecdf1eed9c647c887397 100644
> > --- a/rust/kernel/alloc/kbox.rs
> > +++ b/rust/kernel/alloc/kbox.rs
> > @@ -32,6 +32,8 @@
> > ///
> > /// When dropping a [`Box`], the value is also dropped and the heap memory is automatically freed.
> > ///
> > +/// [`Box`]es can also be used to store trait objects by coercing their type.
> > +///
> > /// # Examples
> > ///
> > /// ```
> > @@ -62,7 +64,17 @@
> > /// `self.0` is always properly aligned and either points to memory allocated with `A` or, for
> > /// zero-sized types, is a dangling, well aligned pointer.
> > #[repr(transparent)]
> > -pub struct Box<T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
> > +#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))]
> > +pub struct Box<#[pointee] T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
> > +
> > +// This is to allow coercion from `Box<T>` to `Box<U>` if `T` can be converted to the
> > +// dynamically-sized type (DST) `U`.
> > +#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
> > +impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Box<U>> for Box<T> {}
>
> You forgot to add the `A: Allocator` generic here and in the impl below.
Nit: Please use a where clause instead; I think it reads better and conforms
with the style of the rest of the file.
prev parent reply other threads:[~2025-04-08 13:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-08 5:18 [PATCH] rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U Alexandre Courbot
2025-04-08 10:22 ` Benno Lossin
2025-04-08 10:35 ` Miguel Ojeda
2025-04-08 13:19 ` Alexandre Courbot
2025-04-08 13:40 ` Alice Ryhl
2025-04-08 13:55 ` Alexandre Courbot
2025-04-08 13:58 ` Miguel Ojeda
2025-04-11 12:01 ` Alexandre Courbot
2025-04-08 13:03 ` Danilo Krummrich [this message]
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=Z_UekWa9F8w_tmuy@pollux \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.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=linux-kernel@vger.kernel.org \
--cc=ojeda@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.