All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: Matthew Maurer <mmaurer@google.com>,
	rust-for-linux@vger.kernel.org,  linux-kernel@vger.kernel.org,
	Alice Ryhl <aliceryhl@google.com>
Subject: [PATCH v2 6/7] rust: alloc: add Vec::remove
Date: Fri, 21 Mar 2025 12:10:01 +0000	[thread overview]
Message-ID: <20250321-vec-methods-v2-6-6d9c8a4634cb@google.com> (raw)
In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com>

This is needed by Rust Binder in the range allocator, and by upcoming
GPU drivers during firmware initialization.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/alloc/kvec.rs | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 866369406ea95e68adea366828552e76e451e24f..f7f7f9c650f8167ad6f53b0d83e328203445aa1f 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -356,6 +356,34 @@ pub fn pop(&mut self) -> Option<T> {
         Some(unsafe { self.as_mut_ptr().add(len_sub_1).read() })
     }
 
+    /// Removes the element at the given index.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut v = kernel::kvec![1, 2, 3]?;
+    /// assert_eq!(v.remove(1), 2);
+    /// assert_eq!(v, [1, 3]);
+    /// # Ok::<(), Error>(())
+    /// ```
+    pub fn remove(&mut self, i: idx) -> T {
+        // INVARIANT: This breaks the invariants by invalidating the value at index `i`, but we
+        // restore the invariants below.
+        // SAFETY: Since `&self[i]` did not result in a panic, the value at index `i` is valid.
+        let value = unsafe { ptr::read(&self[i]) };
+
+        // SAFETY: We checked that `i` is in-bounds.
+        let p = unsafe { self.as_mut_ptr().add(i) };
+        // INVARIANT: The invariants are still broken, but now the invalid value is the last
+        // element of the vector.
+        // SAFETY: `p.add(1).add(self.len - i - 1)` is `i+1+len-i-1 == len` elements after the
+        // beginning of the vector, so this is in-bounds of the vector.
+        unsafe { ptr::copy(p.add(1), p, self.len - i - 1) };
+        // INVARIANT: This restores the invariants since the invalid element no longer needs to be
+        // valid.
+        self.len = self.len - 1;
+    }
+
     /// Creates a new [`Vec`] instance with at least the given capacity.
     ///
     /// # Examples

-- 
2.49.0.395.g12beb8f557-goog


  parent reply	other threads:[~2025-03-21 12:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-21 12:09 [PATCH v2 0/7] Additional methods for Vec Alice Ryhl
2025-03-21 12:09 ` [PATCH v2 1/7] rust: alloc: add Vec::clear Alice Ryhl
2025-03-21 12:09 ` [PATCH v2 2/7] rust: alloc: add Vec::pop Alice Ryhl
2025-03-21 12:09 ` [PATCH v2 3/7] rust: alloc: add Vec::push_within_capacity Alice Ryhl
2025-03-21 12:09 ` [PATCH v2 4/7] rust: alloc: add Vec::drain_all Alice Ryhl
2025-03-21 12:10 ` [PATCH v2 5/7] rust: alloc: add Vec::retain Alice Ryhl
2025-03-21 12:10 ` Alice Ryhl [this message]
2025-03-21 12:10 ` [PATCH v2 7/7] rust: alloc: add Vec::insert_within_capacity Alice Ryhl
2025-03-21 12:25 ` [PATCH v2 0/7] Additional methods for Vec Danilo Krummrich
2025-03-21 13:10   ` Benno Lossin
2025-03-21 22:10     ` Danilo Krummrich
2025-03-21 12:28 ` Alice Ryhl
2025-03-21 15:47 ` Tamir Duberstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250321-vec-methods-v2-6-6d9c8a4634cb@google.com \
    --to=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mmaurer@google.com \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.