Re: [PATCH v2 1/2] rust: add basic Pin<Vec<T, A>> abstractions

From: Alice Ryhl
Date: Fri Oct 10 2025 - 07:33:37 EST


On Thu, Oct 09, 2025 at 06:12:33PM +0000, Markus Probst wrote:
> Implement core Pin<Vec<T, A>> abstractions, including
> * `Vec::pin` and `Vec::into_pin` for constructing a `Pin<Vec<T, A>>`.
> If T does not implement `Unpin`, its values will never be moved.
> * an extension for `Pin<Vec<T, A>>` allowing PinInit to be initialied on a
> Pin<Vec>, as well as truncating and popping values from the Vec
>
> Signed-off-by: Markus Probst <markus.probst@xxxxxxxxx>
> ---
> rust/kernel/alloc.rs | 1 +
> rust/kernel/alloc/kvec.rs | 86 +++++++++++++++++++++++++++++++++++++++
> rust/kernel/prelude.rs | 2 +-
> 3 files changed, 88 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
> index a2c49e5494d3..9c129eaf0625 100644
> --- a/rust/kernel/alloc.rs
> +++ b/rust/kernel/alloc.rs
> @@ -24,6 +24,7 @@
> pub use self::kvec::KVec;
> pub use self::kvec::VVec;
> pub use self::kvec::Vec;
> +pub use self::kvec::PinnedVecExt;
>
> /// Indicates an allocation error.
> #[derive(Copy, Clone, PartialEq, Eq, Debug)]
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 3c72e0bdddb8..d5582a7f17e9 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -16,11 +16,13 @@
> ops::DerefMut,
> ops::Index,
> ops::IndexMut,
> + pin::Pin,
> ptr,
> ptr::NonNull,
> slice,
> slice::SliceIndex,
> };
> +use pin_init::PinInit;
>
> mod errors;
> pub use self::errors::{InsertError, PushError, RemoveError};
> @@ -109,6 +111,21 @@ pub struct Vec<T, A: Allocator> {
> _p: PhantomData<A>,
> }
>
> +/// Extension for Pin<Vec<T, A>>
> +pub trait PinnedVecExt<T> {
> + /// Pin-initializes P and appends it to the back of the [`Vec`] instance without reallocating.
> + fn push_pin_init<E: From<PushError<P>>, P: PinInit<T, E>>(&mut self, init: P) -> Result<(), E>;
> +
> + /// Shortens the vector, setting the length to `len` and drops the removed values.
> + /// If `len` is greater than or equal to the current length, this does nothing.
> + ///
> + /// This has no effect on the capacity and will not allocate.
> + fn truncate(&mut self, len: usize);
> +
> + /// Removes the last element from a vector and drops it returning true, or false if it is empty.
> + fn pop(&mut self) -> bool;
> +}

Please no extension traits just for this. Just provide `self: Pin<&mut Self>`
methods on Vec.

Alice