rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U
Subsystems (2)
Alexandre Courbot
reviewerPATCH v3 1/1 rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U
This enables the creation of trait objects backed by a Box, similarly to what can be done with the standard library.
---
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
---
Changes in v3:
- Added trait object creation example.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Use where clauses to improve readability.
- Fix build with rustc 1.78.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/kernel/alloc/kbox.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)
rust/kernel/alloc/kbox.rs +39 -1
@@ -57,12 +57,50 @@/// assert!(KVBox::<Huge>::new_uninit(GFP_KERNEL).is_ok()); /// ``` /// +/// [`Box`]es can also be used to store trait objects by coercing their type: +/// +/// ``` +/// trait FooTrait {} +/// +/// struct FooStruct; +/// impl FooTrait for FooStruct {} +/// +/// let _ = KBox::new(FooStruct, GFP_KERNEL)? as KBox<dyn FooTrait>; +/// # Ok::<(), Error>(()) +/// ``` +/// /// # Invariants /// /// `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 <#[cfg_attr( CONFIG_RUSTC_HAS_COERCE_POINTEE , pointee)] T : ?Sized ,A :Allocator >( +NonNull <T >, +PhantomData <A >, +); + +// This is to allow coercion from `Box<T, A>` to `Box<U, A>` if `T` can be converted to the +// dynamically-sized type (DST) `U`. +#[cfg(not( CONFIG_RUSTC_HAS_COERCE_POINTEE ))] +impl <T ,U ,A > core::ops::CoerceUnsized <Box <U ,A >>for Box <T ,A > +where +T : ?Sized + core::marker::Unsize <U >, +U : ?Sized , +A :Allocator , +{ +} + +// This is to allow `Box<U, A>` to be dispatched on when `Box<T, A>` can be coerced into `Box<U, +// A>`. +#[cfg(not( CONFIG_RUSTC_HAS_COERCE_POINTEE ))] +impl <T ,U ,A > core::ops::DispatchFromDyn <Box <U ,A >>for Box <T ,A > +where +T : ?Sized + core::marker::Unsize <U >, +U : ?Sized , +A :Allocator , +{ +}/// Type alias for [`Box`] with a [`Kmalloc`] allocator. ///
---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250408-box_trait_objs-02a700401f0b
Best regards,
Signature · 1 line
Alexandre Courbot <[email protected]>
Quoted 7 lines · Alexandre Courbot
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 <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Benno Lossin <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>
Applied to alloc-next, thanks!