diff options
author | Alice Ryhl <aliceryhl@google.com> | 2025-05-02 13:19:34 +0000 |
---|---|---|
committer | Danilo Krummrich <dakr@kernel.org> | 2025-05-07 18:40:02 +0200 |
commit | 294a7ecbdf0a5d65c6df1287c5d56241e9331cf2 (patch) | |
tree | f65ebee141e217d34f474106b2c3372c207b0afe /rust/kernel/alloc/kvec/errors.rs | |
parent | 9f140894e72735f034fdc0e963d0550ef03c6f44 (diff) |
rust: alloc: add Vec::remove
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>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250502-vec-methods-v5-6-06d20ad9366f@google.com
[ Remove `# Panics` section; `Vec::remove() handles the error properly.`
- Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel/alloc/kvec/errors.rs')
-rw-r--r-- | rust/kernel/alloc/kvec/errors.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/rust/kernel/alloc/kvec/errors.rs b/rust/kernel/alloc/kvec/errors.rs index 84c96ec5007d..06fe696e8bc6 100644 --- a/rust/kernel/alloc/kvec/errors.rs +++ b/rust/kernel/alloc/kvec/errors.rs @@ -21,3 +21,18 @@ impl<T> From<PushError<T>> for Error { EINVAL } } + +/// Error type for [`Vec::remove`]. +pub struct RemoveError; + +impl Debug for RemoveError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "Index out of bounds") + } +} + +impl From<RemoveError> for Error { + fn from(_: RemoveError) -> Error { + EINVAL + } +} |