Re: [PATCH v5 6/7] rust: alloc: add Vec::remove

Alexandre Courbot
2025-05-07T05:30:54Z

Hi Alice,

On Fri May 2, 2025 at 10:19 PM JST, Alice Ryhl wrote:
> This is needed by Rust Binder in the range allocator, and by upcoming
> GPU drivers during firmware initialization.
>
> Panics in the kernel are best avoided when possible, so an error is
> returned if the index is out of bounds. An error type is used rather
> than just returning Option<T> to let callers handle errors with ?.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> rust/kernel/alloc/kvec.rs | 42 +++++++++++++++++++++++++++++++++++++++-
> rust/kernel/alloc/kvec/errors.rs | 15 ++++++++++++++
> 2 files changed, 56 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 3298b3b0f32c70f3fe517fcb7af6b9922fea926b..8845e7694334b672476ff935580f3a9eb94d23fe 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -22,7 +22,7 @@
> };
>
> mod errors;
> -pub use self::errors::PushError;
> +pub use self::errors::{PushError, RemoveError};
>
> /// Create a [`KVec`] containing the arguments.
> ///
> @@ -389,6 +389,46 @@ pub fn pop(&mut self) -> Option<T> {
> Some(unsafe { removed.read() })
> }
>
> + /// Removes the element at the given index.
> + ///
> + /// # Panics
> + ///
> + /// Panics if the index is out of bounds.

According to the commit log (and the code of the method) I think this
panic section is not valid anymore?