Skip to content

Commit 22c3335

Browse files
committed
Merge tag 'alloc-next-v6.16-2025-05-13' of https://github.com/Rust-for-Linux/linux into rust-next
Pull alloc updates from Danilo Krummrich: "Box: - Support for type coercion, e.g. 'Box<T>' to 'Box<dyn U>' if T implements U Vec: - Implement new methods (prerequisites for nova-core and binder) - Vec::truncate() - Vec::resize() - Vec::clear() - Vec::pop() - Vec::push_within_capacity() - New error type: PushError - Vec::drain_all() - Vec::retain() - Vec::remove() - New error type: RemoveError - Vec::insert_within_capacity - New error type: InsertError - Simplify Vec::push() using Vec::spare_capacity_mut() - Split Vec::set_len() into Vec::inc_len() and Vec::dec_len() - Add type invariant Vec::len() <= Vec::capacity - Simplify Vec::truncate() using Vec::dec_len()" * tag 'alloc-next-v6.16-2025-05-13' of https://github.com/Rust-for-Linux/linux: rust: alloc: add Vec::insert_within_capacity rust: alloc: add Vec::remove rust: alloc: add Vec::retain rust: alloc: add Vec::drain_all rust: alloc: add Vec::push_within_capacity rust: alloc: add Vec::pop rust: alloc: add Vec::clear rust: alloc: replace `Vec::set_len` with `inc_len` rust: alloc: refactor `Vec::truncate` using `dec_len` rust: alloc: add `Vec::dec_len` rust: alloc: add Vec::len() <= Vec::capacity invariant rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U rust: alloc: use `spare_capacity_mut` to reduce unsafe rust: alloc: add Vec::resize method rust: alloc: add Vec::truncate method rust: alloc: add missing invariant in Vec::set_len()
2 parents b04d170 + 771c5a7 commit 22c3335

File tree

5 files changed

+506
-29
lines changed

5 files changed

+506
-29
lines changed

rust/kernel/alloc/kbox.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,50 @@ use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
5757
/// assert!(KVBox::<Huge>::new_uninit(GFP_KERNEL).is_ok());
5858
/// ```
5959
///
60+
/// [`Box`]es can also be used to store trait objects by coercing their type:
61+
///
62+
/// ```
63+
/// trait FooTrait {}
64+
///
65+
/// struct FooStruct;
66+
/// impl FooTrait for FooStruct {}
67+
///
68+
/// let _ = KBox::new(FooStruct, GFP_KERNEL)? as KBox<dyn FooTrait>;
69+
/// # Ok::<(), Error>(())
70+
/// ```
71+
///
6072
/// # Invariants
6173
///
6274
/// `self.0` is always properly aligned and either points to memory allocated with `A` or, for
6375
/// zero-sized types, is a dangling, well aligned pointer.
6476
#[repr(transparent)]
65-
pub struct Box<T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
77+
#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))]
78+
pub struct Box<#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, pointee)] T: ?Sized, A: Allocator>(
79+
NonNull<T>,
80+
PhantomData<A>,
81+
);
82+
83+
// This is to allow coercion from `Box<T, A>` to `Box<U, A>` if `T` can be converted to the
84+
// dynamically-sized type (DST) `U`.
85+
#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
86+
impl<T, U, A> core::ops::CoerceUnsized<Box<U, A>> for Box<T, A>
87+
where
88+
T: ?Sized + core::marker::Unsize<U>,
89+
U: ?Sized,
90+
A: Allocator,
91+
{
92+
}
93+
94+
// This is to allow `Box<U, A>` to be dispatched on when `Box<T, A>` can be coerced into `Box<U,
95+
// A>`.
96+
#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
97+
impl<T, U, A> core::ops::DispatchFromDyn<Box<U, A>> for Box<T, A>
98+
where
99+
T: ?Sized + core::marker::Unsize<U>,
100+
U: ?Sized,
101+
A: Allocator,
102+
{
103+
}
66104

67105
/// Type alias for [`Box`] with a [`Kmalloc`] allocator.
68106
///

0 commit comments

Comments
 (0)